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
|
/* -*- mode: c; c-file-style: "bsd"; -*- */
/*
Copyright (C) 2001-2003 Paul Davis
Copyright (C) 2005 Jussi Laako
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <regex.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <jack/jack.h>
#include <jack/jslist.h>
#include <jack/thread.h>
#include <jack/uuid.h>
#include "internal.h"
#include "engine.h"
#include "pool.h"
#include "version.h"
#include "shm.h"
#include "unlock.h"
#include "varargs.h"
#include "intsimd.h"
#include "messagebuffer.h"
#include <sysdeps/time.h>
#include "local.h"
#include <sysdeps/poll.h>
#include <sysdeps/ipc.h>
#ifdef JACK_USE_MACH_THREADS
#include <sysdeps/pThreadUtilities.h>
#endif
static pthread_mutex_t client_lock;
static pthread_cond_t client_ready;
static int
jack_client_close_aux(jack_client_t *client);
#define EVENT_POLL_INDEX 0
#define WAIT_POLL_INDEX 1
#define event_fd pollfd[EVENT_POLL_INDEX].fd
#define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
typedef struct {
int status;
struct _jack_client *client;
const char *client_name;
} client_info;
#ifdef USE_DYNSIMD
#ifdef ARCH_X86
int cpu_type = 0;
static void
init_cpu ()
{
cpu_type = ((have_3dnow () << 8) | have_sse ());
#if 0
if (ARCH_X86_HAVE_3DNOW (cpu_type)) {
jack_debug ("Enhanced3DNow! detected");
}
if (ARCH_X86_HAVE_SSE2 (cpu_type)) {
jack_debug ("SSE2 detected");
}
if ((!ARCH_X86_HAVE_3DNOW (cpu_type)) && (!ARCH_X86_HAVE_SSE2 (cpu_type))) {
jack_debug ("No supported SIMD instruction sets detected");
}
#endif
jack_port_set_funcs ();
}
#else /* ARCH_X86 */
static void
init_cpu ()
{
jack_port_set_funcs ();
}
#endif /* ARCH_X86 */
#endif /* USE_DYNSIMD */
/*
* The following read/write wrappers handle the case of interruption
* by system signals:
*/
static int
read_retry(int fd, void *dst, int size)
{
int error;
do {
error = read(fd, dst, size);
} while (error == -1 && errno == EINTR);
return (error);
}
static int
write_retry(int fd, const void *src, int size)
{
int error;
do {
error = write(fd, src, size);
} while (error == -1 && errno == EINTR);
return (error);
}
const char *
jack_get_tmpdir ()
{
static char tmpdir[PATH_MAX + 1] = "";
FILE* in;
size_t len;
char buf[PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
char *pathenv;
char *pathcopy;
char *p;
/* return tmpdir if set */
if (tmpdir[0] != '\0') {
return tmpdir;
}
/* some implementations of popen(3) close a security loophole by
resetting PATH for the exec'd command. since we *want* to
use the user's PATH setting to locate jackd, we have to
do it ourselves.
*/
if ((pathenv = getenv ("PATH")) == 0) {
return NULL;
}
/* don't let strtok(3) mess with the real environment variable */
if ((pathcopy = strdup (pathenv)) == NULL) {
return NULL;
}
p = strtok (pathcopy, ":");
while (p) {
char jackd[PATH_MAX + 1];
char command[PATH_MAX + 4];
snprintf (jackd, sizeof(jackd), "%s/jackd", p);
if (access (jackd, X_OK) == 0) {
snprintf (command, sizeof(command), "%s -l", jackd);
if ((in = popen (command, "r")) != NULL) {
break;
}
}
p = strtok (NULL, ":");
}
if (p == NULL) {
/* no command successfully started */
free (pathcopy);
return NULL;
}
if (fgets (buf, sizeof(buf), in) == NULL) {
pclose (in);
free (pathcopy);
return NULL;
}
len = strlen (buf);
if (buf[len - 1] != '\n') {
/* didn't get a whole line */
pclose (in);
free (pathcopy);
return NULL;
}
memcpy (tmpdir, buf, len - 1);
tmpdir[len - 1] = '\0';
pclose (in);
free (pathcopy);
return tmpdir;
}
void
jack_error (const char *fmt, ...)
{
va_list ap;
char buffer[300];
va_start (ap, fmt);
vsnprintf (buffer, sizeof(buffer), fmt, ap);
jack_error_callback (buffer);
va_end (ap);
}
void
default_jack_error_callback (const char *desc)
{
fprintf (stderr, "%s\n", desc);
fflush (stderr);
}
void
default_jack_info_callback (const char *desc)
{
fprintf (stdout, "%s\n", desc);
fflush (stdout);
}
void
silent_jack_error_callback (const char *desc)
{
}
void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;
void
jack_info (const char *fmt, ...)
{
va_list ap;
char buffer[300];
va_start (ap, fmt);
vsnprintf (buffer, sizeof(buffer), fmt, ap);
jack_info_callback (buffer);
va_end (ap);
}
static int
oop_client_deliver_request (void *ptr, jack_request_t *req)
{
int wok, rok;
jack_client_t *client = (jack_client_t*)ptr;
wok = (write_retry (client->request_fd, req, sizeof(*req))
== sizeof(*req));
/* if necessary, add variable length key data after a PropertyChange request
*/
if (req->type == PropertyChangeNotify) {
if (req->x.property.keylen) {
if (write_retry (client->request_fd, req->x.property.key, req->x.property.keylen) != req->x.property.keylen) {
jack_error ("cannot send property key of length %d to server",
req->x.property.keylen);
req->status = -1;
return req->status;
}
}
}
rok = (read_retry (client->request_fd, req, sizeof(*req))
== sizeof(*req));
if (wok && rok) { /* everything OK? */
return req->status;
}
req->status = -1; /* request failed */
/* check for server shutdown */
if (client->engine->engine_ok == 0) {
return req->status;
}
/* otherwise report errors */
if (!wok) {
jack_error ("cannot send request type %d to server",
req->type);
}
if (!rok) {
jack_error ("cannot read result for request type %d from"
" server (%s)", req->type, strerror (errno));
}
return req->status;
}
int
jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
{
/* indirect through the function pointer that was set either
* by jack_client_open() or by jack_new_client_request() in
* the server.
*/
return client->deliver_request (client->deliver_arg, req);
}
#if JACK_USE_MACH_THREADS
jack_client_t *
jack_client_alloc ()
{
jack_client_t *client;
if ((client = (jack_client_t*)calloc (1, sizeof(jack_client_t))) == NULL) {
return NULL;
}
if ((client->pollfd = (struct pollfd*)malloc (sizeof(struct pollfd) * 1)) == NULL) {
free (client);
return NULL;
}
client->pollmax = 1;
client->request_fd = -1;
client->event_fd = -1;
client->upstream_is_jackd = 0;
client->graph_next_fd = -1;
client->ports = NULL;
client->ports_ext = NULL;
client->engine = NULL;
client->control = NULL;
client->thread_ok = FALSE;
client->rt_thread_ok = FALSE;
client->first_active = TRUE;
client->on_shutdown = NULL;
client->on_info_shutdown = NULL;
client->n_port_types = 0;
client->port_segment = NULL;
#ifdef USE_DYNSIMD
init_cpu ();
#endif /* USE_DYNSIMD */
return client;
}
#else
jack_client_t *
jack_client_alloc ()
{
jack_client_t *client;
if ((client = (jack_client_t*)calloc (1, sizeof(jack_client_t))) == NULL) {
return NULL;
}
if ((client->pollfd = (struct pollfd*)malloc (sizeof(struct pollfd) * 2)) == NULL) {
free (client);
return NULL;
}
client->pollmax = 2;
client->request_fd = -1;
client->event_fd = -1;
client->upstream_is_jackd = 0;
client->graph_wait_fd = -1;
client->graph_next_fd = -1;
client->ports = NULL;
client->ports_ext = NULL;
client->engine = NULL;
client->control = NULL;
client->thread_ok = FALSE;
client->first_active = TRUE;
client->on_shutdown = NULL;
client->on_info_shutdown = NULL;
client->n_port_types = 0;
client->port_segment = NULL;
#ifdef USE_DYNSIMD
init_cpu ();
#endif /* USE_DYNSIMD */
return client;
}
#endif
/*
* Build the jack_client_t structure for an internal client.
*/
jack_client_t *
jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
{
jack_client_t* client;
client = jack_client_alloc ();
client->control = cc;
client->engine = engine->control;
client->n_port_types = client->engine->n_port_types;
client->port_segment = &engine->port_segment[0];
return client;
}
static void
jack_client_free (jack_client_t *client)
{
if (client->pollfd) {
free (client->pollfd);
}
free (client);
}
void
jack_client_fix_port_buffers (jack_client_t *client)
{
JSList *node;
jack_port_t *port;
/* This releases all local memory owned by input ports
and sets the buffer pointer to NULL. This will cause
jack_port_get_buffer() to reallocate space for the
buffer on the next call (if there is one).
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
port = (jack_port_t*)node->data;
if (port->shared->flags & JackPortIsInput) {
if (port->mix_buffer) {
size_t buffer_size =
jack_port_type_buffer_size ( port->type_info,
client->engine->buffer_size );
jack_pool_release (port->mix_buffer);
port->mix_buffer = NULL;
pthread_mutex_lock (&port->connection_lock);
if (jack_slist_length (port->connections) > 1) {
port->mix_buffer = jack_pool_alloc (buffer_size);
port->fptr.buffer_init (port->mix_buffer,
buffer_size,
client->engine->buffer_size);
}
pthread_mutex_unlock (&port->connection_lock);
}
}
}
}
int
jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
{
jack_port_t *control_port;
jack_port_t *other = 0;
JSList *node;
int need_free = FALSE;
if (jack_uuid_compare (client->engine->ports[event->x.self_id].client_id, client->control->uuid) == 0 ||
jack_uuid_compare (client->engine->ports[event->y.other_id].client_id, client->control->uuid) == 0) {
/* its one of ours */
switch (event->type) {
case PortConnected:
other = jack_port_new (client, event->y.other_id,
client->engine);
/* jack_port_by_id_int() always returns an internal
* port that does not need to be deallocated
*/
control_port = jack_port_by_id_int (client, event->x.self_id,
&need_free);
pthread_mutex_lock (&control_port->connection_lock);
if ((control_port->shared->flags & JackPortIsInput)
&& (control_port->connections != NULL)
&& (control_port->mix_buffer == NULL) ) {
size_t buffer_size =
jack_port_type_buffer_size ( control_port->type_info,
client->engine->buffer_size );
control_port->mix_buffer = jack_pool_alloc (buffer_size);
control_port->fptr.buffer_init (control_port->mix_buffer,
buffer_size,
client->engine->buffer_size);
}
control_port->connections =
jack_slist_prepend (control_port->connections,
(void*)other);
pthread_mutex_unlock (&control_port->connection_lock);
break;
case PortDisconnected:
/* jack_port_by_id_int() always returns an internal
* port that does not need to be deallocated
*/
control_port = jack_port_by_id_int (client, event->x.self_id,
&need_free);
pthread_mutex_lock (&control_port->connection_lock);
for (node = control_port->connections; node;
node = jack_slist_next (node)) {
other = (jack_port_t*)node->data;
if (other->shared->id == event->y.other_id) {
control_port->connections =
jack_slist_remove_link (
control_port->connections,
node);
jack_slist_free_1 (node);
free (other);
break;
}
}
pthread_mutex_unlock (&control_port->connection_lock);
break;
default:
/* impossible */
break;
}
}
if (client->control->port_connect_cbset) {
client->port_connect (event->x.self_id, event->y.other_id,
(event->type == PortConnected ? 1 : 0),
client->port_connect_arg);
}
return 0;
}
int
jack_client_handle_session_callback (jack_client_t *client, jack_event_t *event)
{
char uuidstr[37];
jack_session_event_t *s_event;
if (!client->control->session_cbset) {
return -1;
}
jack_uuid_unparse (client->control->uuid, uuidstr);
s_event = malloc ( sizeof(jack_session_event_t) );
s_event->type = event->y.n;
s_event->session_dir = strdup (event->x.name);
s_event->client_uuid = strdup (uuidstr);
s_event->command_line = NULL;
s_event->future = 0;
client->session_cb_immediate_reply = 0;
client->session_cb (s_event, client->session_cb_arg);
if (client->session_cb_immediate_reply) {
return 2;
}
return 1;
}
static void
jack_port_recalculate_latency (jack_port_t *port, jack_latency_callback_mode_t mode)
{
jack_latency_range_t latency = { UINT32_MAX, 0 };
JSList *node;
pthread_mutex_lock (&port->connection_lock);
for (node = port->connections; node; node = jack_slist_next (node)) {
jack_port_t *other = node->data;
jack_latency_range_t other_latency;
jack_port_get_latency_range (other, mode, &other_latency);
if (other_latency.max > latency.max) {
latency.max = other_latency.max;
}
if (other_latency.min < latency.min) {
latency.min = other_latency.min;
}
}
pthread_mutex_unlock (&port->connection_lock);
if (latency.min == UINT32_MAX) {
latency.min = 0;
}
jack_port_set_latency_range (port, mode, &latency);
}
int
jack_client_handle_latency_callback (jack_client_t *client, jack_event_t *event, int is_driver)
{
jack_latency_callback_mode_t mode = (event->x.n == 0) ? JackCaptureLatency : JackPlaybackLatency;
JSList *node;
jack_latency_range_t latency = { UINT32_MAX, 0 };
/* first setup all latency values of the ports.
* this is based on the connections of the ports.
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
jack_port_t *port = node->data;
if ((jack_port_flags (port) & JackPortIsOutput) && (mode == JackPlaybackLatency)) {
jack_port_recalculate_latency (port, mode);
}
if ((jack_port_flags (port) & JackPortIsInput) && (mode == JackCaptureLatency)) {
jack_port_recalculate_latency (port, mode);
}
}
/* for a driver invocation without its own latency callback, this is enough.
* input and output ports do not depend on each other.
*/
if (is_driver && !client->control->latency_cbset) {
return 0;
}
if (!client->control->latency_cbset) {
/*
* default action is to assume all ports depend on each other.
* then always take the maximum latency.
*/
if (mode == JackPlaybackLatency) {
/* iterate over all OutputPorts, to find maximum playback latency
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
jack_port_t *port = node->data;
if (port->shared->flags & JackPortIsOutput) {
jack_latency_range_t other_latency;
jack_port_get_latency_range (port, mode, &other_latency);
if (other_latency.max > latency.max) {
latency.max = other_latency.max;
}
if (other_latency.min < latency.min) {
latency.min = other_latency.min;
}
}
}
if (latency.min == UINT32_MAX) {
latency.min = 0;
}
/* now set the found latency on all input ports
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
jack_port_t *port = node->data;
if (port->shared->flags & JackPortIsInput) {
jack_port_set_latency_range (port, mode, &latency);
}
}
}
if (mode == JackCaptureLatency) {
/* iterate over all InputPorts, to find maximum playback latency
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
jack_port_t *port = node->data;
if (port->shared->flags & JackPortIsInput) {
jack_latency_range_t other_latency;
jack_port_get_latency_range (port, mode, &other_latency);
if (other_latency.max > latency.max) {
latency.max = other_latency.max;
}
if (other_latency.min < latency.min) {
latency.min = other_latency.min;
}
}
}
if (latency.min == UINT32_MAX) {
latency.min = 0;
}
/* now set the found latency on all output ports
*/
for (node = client->ports; node; node = jack_slist_next (node)) {
jack_port_t *port = node->data;
if (port->shared->flags & JackPortIsOutput) {
jack_port_set_latency_range (port, mode, &latency);
}
}
}
return 0;
}
/* we have a latency callback setup by the client,
* lets use it...
*/
client->latency_cb ( mode, client->latency_cb_arg);
return 0;
}
#if JACK_USE_MACH_THREADS
static int
jack_handle_reorder (jack_client_t *client, jack_event_t *event)
{
client->pollmax = 1;
/* If the client registered its own callback for graph order events,
execute it now.
*/
if (client->control->graph_order_cbset) {
client->graph_order (client->graph_order_arg);
}
return 0;
}
#else
static int
jack_handle_reorder (jack_client_t *client, jack_event_t *event)
{
char path[PATH_MAX + 1];
DEBUG ("graph reorder\n");
if (client->graph_wait_fd >= 0) {
DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
close (client->graph_wait_fd);
client->graph_wait_fd = -1;
}
if (client->graph_next_fd >= 0) {
DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
close (client->graph_next_fd);
client->graph_next_fd = -1;
}
sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
if ((client->graph_wait_fd = open (path, O_RDONLY | O_NONBLOCK)) < 0) {
jack_error ("cannot open specified fifo [%s] for reading (%s)",
path, strerror (errno));
return -1;
}
DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n + 1);
if ((client->graph_next_fd = open (path, O_WRONLY | O_NONBLOCK)) < 0) {
jack_error ("cannot open specified fifo [%s] for writing (%s)",
path, strerror (errno));
return -1;
}
client->upstream_is_jackd = event->y.n;
client->pollmax = 2;
DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
client->graph_next_fd, path,
client->upstream_is_jackd);
/* If the client registered its own callback for graph order events,
execute it now.
*/
if (client->control->graph_order_cbset) {
client->graph_order (client->graph_order_arg);
}
return 0;
}
#endif
static int
server_connect (const char *server_name)
{
int fd;
struct sockaddr_un addr;
int which = 0;
char server_dir[PATH_MAX + 1] = "";
if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
jack_error ("cannot create client socket (%s)",
strerror (errno));
return -1;
}
//JOQ: temporary debug message
//jack_info ("DEBUG: connecting to `%s' server", server_name);
addr.sun_family = AF_UNIX;
snprintf (addr.sun_path, sizeof(addr.sun_path) - 1, "%s/jack_%d",
jack_server_dir (server_name, server_dir), which);
if (connect (fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close (fd);
jack_error ("connect(2) call to %s failed (err=%s)", addr.sun_path, strerror (errno));
return -1;
}
return fd;
}
static int
server_event_connect (jack_client_t *client, const char *server_name)
{
int fd;
struct sockaddr_un addr;
jack_client_connect_ack_request_t req;
jack_client_connect_ack_result_t res;
char server_dir[PATH_MAX + 1] = "";
if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
jack_error ("cannot create client event socket (%s)",
strerror (errno));
return -1;
}
addr.sun_family = AF_UNIX;
snprintf (addr.sun_path, sizeof(addr.sun_path) - 1, "%s/jack_ack_0",
jack_server_dir (server_name, server_dir));
if (connect (fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
jack_error ("cannot connect to jack server for events",
strerror (errno));
close (fd);
return -1;
}
jack_uuid_copy (&req.client_id, client->control->uuid);
if (write_retry (fd, &req, sizeof(req)) != sizeof(req)) {
jack_error ("cannot write event connect request to server (%s)",
strerror (errno));
close (fd);
return -1;
}
if (read_retry (fd, &res, sizeof(res)) != sizeof(res)) {
jack_error ("cannot read event connect result from server (%s)",
strerror (errno));
close (fd);
return -1;
}
if (res.status != 0) {
jack_error ("cannot connect to server for event stream (%s)",
strerror (errno));
close (fd);
return -1;
}
return fd;
}
/* Exec the JACK server in this process. Does not return. */
static void
_start_server (const char *server_name)
{
FILE* fp = 0;
char filename[255];
char arguments[255];
char buffer[255];
char* command = 0;
size_t pos = 0;
size_t result = 0;
char** argv = 0;
int i = 0;
int good = 0;
int ret;
char *startup_file;
if ((startup_file = getenv ("JACK_RC_FILE")) == NULL) {
snprintf (filename, 255, "%s/.jackdrc", getenv ("HOME"));
startup_file = filename;
}
fp = fopen (startup_file, "r");
if (!fp) {
fp = fopen ("/etc/jackdrc", "r");
}
/* if still not found, check old config name for backwards compatability */
if (!fp) {
fp = fopen ("/etc/jackd.conf", "r");
}
if (fp) {
arguments[0] = '\0';
ret = fscanf (fp, "%s", buffer);
while (ret != 0 && ret != EOF) {
strcat (arguments, buffer);
strcat (arguments, " ");
ret = fscanf (fp, "%s", buffer);
}
if (strlen (arguments) > 0) {
good = 1;
}
}
if (!good) {
#if defined(USE_CAPABILITIES)
command = JACK_LOCATION "/jackstart";
strncpy (arguments, JACK_LOCATION "/jackstart -T -R -d "
JACK_DEFAULT_DRIVER " -p 512", 255);
#else /* !USE_CAPABILITIES */
command = JACK_LOCATION "/jackd";
strncpy (arguments, JACK_LOCATION "/jackd -T -d "
JACK_DEFAULT_DRIVER, 255);
#endif /* USE_CAPABILITIES */
} else {
result = strcspn (arguments, " ");
if ((command = (char*)malloc (result + 1)) == NULL) {
goto failure;
}
strncpy (command, arguments, result);
command[result] = '\0';
}
if ((argv = (char**)malloc (255)) == NULL) {
goto failure;
}
while (1) {
/* insert -T and -nserver_name in front of arguments */
if (i == 1) {
argv[i] = (char*)malloc (strlen ("-T") + 1);
strcpy (argv[i++], "-T");
if (server_name) {
size_t optlen = strlen ("-n");
char *buf =
malloc (optlen
+ strlen (server_name) + 1);
strcpy (buf, "-n");
strcpy (buf + optlen, server_name);
argv[i++] = buf;
}
}
/* skip whitespace */
while (pos < strlen (arguments) && arguments[pos] && arguments[pos] == ' ')
++pos;
if (pos >= strlen (arguments)) {
break;
}
if (arguments[pos] == '\"') {
++pos;
result = strcspn (arguments + pos, "\"");
} else {
result = strcspn (arguments + pos, " ");
}
if (0 == result) {
break;
}
argv[i] = (char*)malloc (result + 1);
strncpy (argv[i], arguments + pos, result);
argv[i][result] = '\0';
pos += result + 1;
if (++i > 253) {
break;
}
}
argv[i] = 0;
#if 0
fprintf (stderr, "execing JACK using %s\n", command);
for (_xx = 0; argv[_xx]; ++_xx)
fprintf (stderr, "\targv[%d] = %s\n", _xx, argv[_xx]);
#endif
execv (command, argv);
failure:
/* If execv() succeeds, it does not return. There's no point
* in calling jack_error() here in the child process. */
fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
}
int
start_server (const char *server_name, jack_options_t options)
{
int status;
pid_t first_child_pid;
if ((options & JackNoStartServer)
|| getenv ("JACK_NO_START_SERVER")) {
return 1;
}
/* The double fork() forces the server to become a child of
* init, which will always clean up zombie process state on
* termination. This even works in cases where the server
* terminates but this client does not.
*
* Since fork() is usually implemented using copy-on-write
* virtual memory tricks, the overhead of the second fork() is
* probably relatively small.
*/
first_child_pid = fork ();
switch (first_child_pid) {
case 0: /* child process */
switch (fork ()) {
case 0: /* grandchild process */
_start_server (server_name);
_exit (99); /* exec failed */
case -1:
_exit (98);
default:
_exit (0);
}
case -1: /* fork() error */
return 1; /* failed to start server */
}
/* reap the initaial child */
waitpid (first_child_pid, &status, 0);
/* only the original parent process goes here */
return 0; /* (probably) successful */
}
static int
jack_request_client (ClientType type,
const char* client_name, jack_options_t options,
jack_status_t *status, jack_varargs_t *va,
jack_client_connect_result_t *res, int *req_fd)
{
jack_client_connect_request_t req;
*req_fd = -1;
memset (&req, 0, sizeof(req));
req.options = options;
if (strlen (client_name) >= sizeof(req.name)) {
jack_error ("\"%s\" is too long to be used as a JACK client"
" name.\n"
"Please use %lu characters or less.",
client_name, sizeof(req.name));
return -1;
}
if (va->load_name
&& (strlen (va->load_name) > sizeof(req.object_path) - 1)) {
jack_error ("\"%s\" is too long to be used as a JACK shared"
" object name.\n"
"Please use %lu characters or less.",
va->load_name, sizeof(req.object_path) - 1);
return -1;
}
if (va->load_init
&& (strlen (va->load_init) > sizeof(req.object_data) - 1)) {
jack_error ("\"%s\" is too long to be used as a JACK shared"
" object data string.\n"
"Please use %lu characters or less.",
va->load_init, sizeof(req.object_data) - 1);
return -1;
}
if ((*req_fd = server_connect (va->server_name)) < 0) {
int trys;
if (start_server (va->server_name, options)) {
*status |= (JackFailure | JackServerFailed);
goto fail;
}
trys = 5;
do {
sleep (1);
if (--trys < 0) {
*status |= (JackFailure | JackServerFailed);
goto fail;
}
} while ((*req_fd = server_connect (va->server_name)) < 0);
*status |= JackServerStarted;
}
/* format connection request */
if (va->sess_uuid && strlen (va->sess_uuid)) {
if (jack_uuid_parse (va->sess_uuid, &req.uuid) != 0) {
jack_error ("Given UUID [%s] is not parseable", va->sess_uuid);
goto fail;
}
} else {
jack_uuid_clear (&req.uuid);
}
req.protocol_v = jack_protocol_version;
req.load = TRUE;
req.type = type;
snprintf (req.name, sizeof(req.name),
"%s", client_name);
snprintf (req.object_path, sizeof(req.object_path),
"%s", va->load_name);
snprintf (req.object_data, sizeof(req.object_data),
"%s", va->load_init);
if (write_retry (*req_fd, &req, sizeof(req)) != sizeof(req)) {
jack_error ("cannot send request to jack server (%s)",
strerror (errno));
*status |= (JackFailure | JackServerError);
goto fail;
}
if (read_retry (*req_fd, res, sizeof(*res)) != sizeof(*res)) {
if (errno == 0) {
/* server shut the socket */
jack_error ("could not attach as client");
*status |= (JackFailure | JackServerError);
goto fail;
}
if (errno == ECONNRESET) {
jack_error ("could not attach as JACK client "
"(server has exited)");
*status |= (JackFailure | JackServerError);
goto fail;
}
jack_error ("cannot read response from jack server (%s)",
strerror (errno));
*status |= (JackFailure | JackServerError);
goto fail;
}
*status |= res->status; /* return server status bits */
if (*status & JackFailure) {
if (*status & JackVersionError) {
jack_error ("client linked with incompatible libjack"
" version.");
}
jack_error ("could not attach to JACK server");
*status |= JackServerError;
goto fail;
}
switch (type) {
case ClientDriver:
case ClientInternal:
close (*req_fd);
*req_fd = -1;
break;
default:
break;
}
return 0;
fail:
jack_error ("attempt to connect to server failed");
if (*req_fd >= 0) {
close (*req_fd);
*req_fd = -1;
}
return -1;
}
int
jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
{
/* Lookup, attach and register the port/buffer segments in use
* right now.
*/
if (client->control->type != ClientExternal) {
jack_error ("Only external clients need attach port segments");
abort ();
}
/* make sure we have space to store the port
segment information.
*/
if (ptid >= client->n_port_types) {
client->port_segment = (jack_shm_info_t*)
realloc (client->port_segment,
sizeof(jack_shm_info_t) * (ptid + 1));
memset (&client->port_segment[client->n_port_types],
0,
sizeof(jack_shm_info_t) *
(ptid - client->n_port_types));
client->n_port_types = ptid + 1;
} else {
/* release any previous segment */
jack_release_shm (&client->port_segment[ptid]);
}
/* get the index into the shm registry */
client->port_segment[ptid].index =
client->engine->port_types[ptid].shm_registry_index;
/* attach the relevant segment */
if (jack_attach_shm (&client->port_segment[ptid])) {
jack_error ("cannot attach port segment shared memory"
" (%s)", strerror (errno));
return -1;
}
return 0;
}
jack_client_t *
jack_client_open_aux (const char *client_name,
jack_options_t options,
jack_status_t *status, va_list ap)
{
/* optional arguments: */
jack_varargs_t va; /* variable arguments */
int req_fd = -1;
int ev_fd = -1;
jack_client_connect_result_t res;
jack_client_t *client;
jack_port_type_id_t ptid;
jack_status_t my_status;
jack_messagebuffer_init ();
if (status == NULL) { /* no status from caller? */
status = &my_status; /* use local status word */
}
*status = 0;
/* validate parameters */
if ((options & ~JackOpenOptions)) {
*status |= (JackFailure | JackInvalidOption);
jack_messagebuffer_exit ();
return NULL;
}
/* parse variable arguments */
jack_varargs_parse (options, ap, &va);
/* External clients need to know where the tmpdir used for
communication with the server lives
*/
if (jack_get_tmpdir () == NULL) {
*status |= JackFailure;
jack_messagebuffer_exit ();
return NULL;
}
/* External clients need this initialized. It is already set
* up in the server's address space for internal clients.
*/
jack_init_time ();
if (jack_request_client (ClientExternal, client_name, options, status,
&va, &res, &req_fd)) {
jack_messagebuffer_exit ();
return NULL;
}
/* Allocate the jack_client_t structure in local memory.
* Shared memory is not accessible yet. */
client = jack_client_alloc ();
strcpy (client->name, res.name);
strcpy (client->fifo_prefix, res.fifo_prefix);
client->request_fd = req_fd;
client->pollfd[EVENT_POLL_INDEX].events =
POLLIN | POLLERR | POLLHUP | POLLNVAL;
#ifndef JACK_USE_MACH_THREADS
client->pollfd[WAIT_POLL_INDEX].events =
POLLIN | POLLERR | POLLHUP | POLLNVAL;
#endif
/* Don't access shared memory until server connected. */
if (jack_initialize_shm (va.server_name)) {
jack_error ("Unable to initialize shared memory.");
*status |= (JackFailure | JackShmFailure);
goto fail;
}
/* attach the engine control/info block */
client->engine_shm.index = res.engine_shm_index;
if (jack_attach_shm (&client->engine_shm)) {
jack_error ("cannot attached engine control shared memory"
" segment");
goto fail;
}
client->engine = (jack_control_t*)jack_shm_addr (&client->engine_shm);
/* initialize clock source as early as possible */
jack_set_clock_source (client->engine->clock_source);
/* now attach the client control block */
client->control_shm.index = res.client_shm_index;
if (jack_attach_shm (&client->control_shm)) {
jack_error ("cannot attached client control shared memory"
" segment");
goto fail;
}
client->control = (jack_client_control_t*)
jack_shm_addr (&client->control_shm);
/* Nobody else needs to access this shared memory any more, so
* destroy it. Because we have it attached, it won't vanish
* till we exit (and release it).
*/
jack_destroy_shm (&client->control_shm);
client->n_port_types = client->engine->n_port_types;
if ((client->port_segment = (jack_shm_info_t*)malloc (sizeof(jack_shm_info_t) * client->n_port_types)) == NULL) {
goto fail;
}
for (ptid = 0; ptid < client->n_port_types; ++ptid) {
client->port_segment[ptid].index =
client->engine->port_types[ptid].shm_registry_index;
client->port_segment[ptid].attached_at = MAP_FAILED;
/* the server will send attach events during jack_activate
*/
}
/* set up the client so that it does the right thing for an
* external client
*/
client->deliver_request = oop_client_deliver_request;
client->deliver_arg = client;
if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
goto fail;
}
client->event_fd = ev_fd;
#ifdef JACK_USE_MACH_THREADS
/* specific resources for server/client real-time thread
* communication */
client->clienttask = mach_task_self ();
if (task_get_bootstrap_port (client->clienttask, &client->bp)) {
jack_error ("Can't find bootstrap port");
goto fail;
}
if (allocate_mach_clientport (client, res.portnum) < 0) {
jack_error ("Can't allocate mach port");
goto fail;
}
;
#endif /* JACK_USE_MACH_THREADS */
return client;
fail:
jack_messagebuffer_exit ();
if (client->engine) {
jack_release_shm (&client->engine_shm);
client->engine = 0;
}
if (client->control) {
jack_release_shm (&client->control_shm);
client->control = 0;
}
if (req_fd >= 0) {
close (req_fd);
}
if (ev_fd >= 0) {
close (ev_fd);
}
free (client);
return NULL;
}
jack_client_t* jack_client_open (const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
{
va_list ap;
va_start (ap, status);
jack_client_t* res = jack_client_open_aux (ext_client_name, options, status, ap);
va_end (ap);
return res;
}
jack_client_t *
jack_client_new (const char *client_name)
{
jack_options_t options = JackUseExactName;
if (getenv ("JACK_START_SERVER") == NULL) {
options |= JackNoStartServer;
}
return jack_client_open (client_name, options, NULL);
}
char *
jack_get_client_name (jack_client_t *client)
{
return client->name;
}
int
jack_internal_client_new (const char *client_name,
const char *so_name, const char *so_data)
{
jack_client_connect_result_t res;
int req_fd;
jack_varargs_t va;
jack_status_t status;
jack_options_t options = JackUseExactName;
if (getenv ("JACK_START_SERVER") == NULL) {
options |= JackNoStartServer;
}
jack_varargs_init (&va);
va.load_name = (char*)so_name;
va.load_init = (char*)so_data;
return jack_request_client (ClientInternal, client_name,
options, &status, &va, &res, &req_fd);
}
char *
jack_default_server_name (void)
{
char *server_name;
if ((server_name = getenv ("JACK_DEFAULT_SERVER")) == NULL) {
server_name = "default";
}
return server_name;
}
/* returns the name of the per-user subdirectory of jack_tmpdir */
char *
jack_user_dir (void)
{
static char user_dir[PATH_MAX + 1] = "";
const char *tmpdir;
/* format the path name on the first call */
if (user_dir[0] == '\0') {
tmpdir = jack_get_tmpdir ();
/* previous behavior of jack_tmpdir, should be changed later */
if (tmpdir == NULL) {
jack_error ("Unable to get tmpdir in user dir");
tmpdir = DEFAULT_TMP_DIR;
}
if (getenv ("JACK_PROMISCUOUS_SERVER")) {
snprintf (user_dir, sizeof(user_dir), "%s/jack",
tmpdir);
} else {
snprintf (user_dir, sizeof(user_dir), "%s/jack-%d",
tmpdir, getuid ());
}
}
return user_dir;
}
/* returns the name of the per-server subdirectory of jack_user_dir() */
char *
jack_server_dir (const char *server_name, char *server_dir)
{
/* format the path name into the suppled server_dir char array,
* assuming that server_dir is at least as large as PATH_MAX+1 */
if (server_name == NULL || server_name[0] == '\0') {
snprintf (server_dir, PATH_MAX + 1, "%s/%s",
jack_user_dir (), jack_default_server_name ());
} else {
snprintf (server_dir, PATH_MAX + 1, "%s/%s",
jack_user_dir (), server_name);
}
return server_dir;
}
void
jack_internal_client_close (const char *client_name)
{
jack_client_connect_request_t req;
int fd;
char *server_name = jack_default_server_name ();
req.load = FALSE;
snprintf (req.name, sizeof(req.name), "%s", client_name);
if ((fd = server_connect (server_name)) < 0) {
return;
}
if (write_retry (fd, &req, sizeof(req)) != sizeof(req)) {
jack_error ("cannot deliver ClientUnload request to JACK "
"server.");
}
/* no response to this request */
close (fd);
return;
}
int
jack_recompute_total_latencies (jack_client_t* client)
{
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = RecomputeTotalLatencies;
return jack_client_deliver_request (client, &request);
}
int
jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
{
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = RecomputeTotalLatency;
request.x.port_info.port_id = port->shared->id;
return jack_client_deliver_request (client, &request);
}
int
jack_set_freewheel (jack_client_t* client, int onoff)
{
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = onoff ? FreeWheel : StopFreeWheel;
jack_uuid_copy (&request.x.client_id, client->control->uuid);
return jack_client_deliver_request (client, &request);
}
int
jack_session_reply (jack_client_t *client, jack_session_event_t *event )
{
int retval = 0;
if (event->command_line) {
snprintf ((char*)client->control->session_command,
sizeof(client->control->session_command),
"%s", event->command_line);
client->control->session_flags = event->flags;
} else {
retval = -1;
}
if (pthread_self () == client->thread_id) {
client->session_cb_immediate_reply = 1;
} else {
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = SessionReply;
jack_uuid_copy (&request.x.client_id, client->control->uuid);
retval = jack_client_deliver_request (client, &request);
}
return retval;
}
void
jack_session_event_free (jack_session_event_t *event)
{
if (event->command_line) {
free (event->command_line);
}
free ((char*)event->session_dir);
free ((char*)event->client_uuid);
free (event);
}
void
jack_session_commands_free (jack_session_command_t *cmds)
{
int i = 0;
while (1) {
if (cmds[i].client_name) {
free ((char*)cmds[i].client_name);
}
if (cmds[i].command) {
free ((char*)cmds[i].command);
}
if (cmds[i].uuid) {
free ((char*)cmds[i].uuid);
} else {
break;
}
i += 1;
}
free (cmds);
}
jack_session_command_t *
jack_session_notify (jack_client_t* client, const char *target, jack_session_event_type_t code, const char *path )
{
jack_request_t request;
jack_session_command_t *retval = NULL;
int num_replies = 0;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = SessionNotify;
if ( path ) {
snprintf ( request.x.session.path, sizeof( request.x.session.path ), "%s", path );
} else {
request.x.session.path[0] = '\0';
}
if ( target ) {
snprintf ( request.x.session.target, sizeof( request.x.session.target ), "%s", target );
} else {
request.x.session.target[0] = '\0';
}
request.x.session.type = code;
if ( (write_retry (client->request_fd, &request, sizeof(request))
!= sizeof(request)) ) {
jack_error ("cannot send request type %d to server",
request.type);
goto out;
}
while ( 1 ) {
jack_uuid_t uid;
if (read_retry (client->request_fd, &uid, sizeof(uid)) != sizeof(uid)) {
jack_error ("cannot read result for request type %d from"
" server (%s)", request.type, strerror (errno));
goto out;
}
num_replies += 1;
retval = realloc ( retval, (num_replies) * sizeof(jack_session_command_t) );
retval[num_replies - 1].client_name = malloc (JACK_CLIENT_NAME_SIZE);
retval[num_replies - 1].command = malloc (JACK_PORT_NAME_SIZE);
retval[num_replies - 1].uuid = malloc (JACK_UUID_STRING_SIZE);
if ( (retval[num_replies - 1].client_name == NULL)
|| (retval[num_replies - 1].command == NULL)
|| (retval[num_replies - 1].uuid == NULL) ) {
goto out;
}
if (jack_uuid_empty (uid)) {
break;
}
if (read_retry (client->request_fd, (char*)retval[num_replies - 1].client_name, JACK_CLIENT_NAME_SIZE)
!= JACK_CLIENT_NAME_SIZE) {
jack_error ("cannot read result for request type %d from"
" server (%s)", request.type, strerror (errno));
goto out;
}
if (read_retry (client->request_fd, (char*)retval[num_replies - 1].command, JACK_PORT_NAME_SIZE)
!= JACK_PORT_NAME_SIZE) {
jack_error ("cannot read result for request type %d from"
" server (%s)", request.type, strerror (errno));
goto out;
}
if (read_retry (client->request_fd, &retval[num_replies - 1].flags, sizeof(retval[num_replies - 1].flags) )
!= sizeof(retval[num_replies - 1].flags) ) {
jack_error ("cannot read result for request type %d from"
" server (%s)", request.type, strerror (errno));
goto out;
}
jack_uuid_unparse (uid, (char*)retval[num_replies - 1].uuid);
}
free ((char*)retval[num_replies - 1].uuid);
retval[num_replies - 1].uuid = NULL;
retval[num_replies - 1].client_name = NULL;
retval[num_replies - 1].command = NULL;
return retval;
out:
if ( retval ) {
jack_session_commands_free (retval);
}
return NULL;
}
int
jack_client_has_session_callback (jack_client_t *client, const char *client_name)
{
jack_request_t request;
int retval;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = SessionHasCallback;
strncpy (request.x.name, client_name, JACK_CLIENT_NAME_SIZE);
retval = jack_client_deliver_request (client, &request);
return retval;
}
void
jack_start_freewheel (jack_client_t* client)
{
jack_client_control_t *control = client->control;
if (client->engine->real_time) {
#if JACK_USE_MACH_THREADS
jack_drop_real_time_scheduling (client->process_thread);
#else
jack_drop_real_time_scheduling (client->thread);
#endif
}
if (control->freewheel_cb_cbset) {
client->freewheel_cb (1, client->freewheel_arg);
}
}
void
jack_stop_freewheel (jack_client_t* client)
{
jack_client_control_t *control = client->control;
if (client->engine->real_time) {
#if JACK_USE_MACH_THREADS
jack_acquire_real_time_scheduling (client->process_thread,
client->engine->client_priority);
#else
jack_acquire_real_time_scheduling (client->thread,
client->engine->client_priority);
#endif
}
if (control->freewheel_cb_cbset) {
client->freewheel_cb (0, client->freewheel_arg);
}
}
static void
jack_client_thread_suicide (jack_client_t* client, const char* reason)
{
#ifdef JACK_USE_MACH_THREADS
client->rt_thread_ok = FALSE;
#endif
if (client->on_info_shutdown) {
jack_error ("%s - calling shutdown handler", reason);
client->on_info_shutdown (JackClientZombie, reason, client->on_info_shutdown_arg);
} else if (client->on_shutdown) {
jack_error ("%s - calling shutdown handler", reason);
client->on_shutdown (client->on_shutdown_arg);
} else {
jack_error ("jack_client_thread: %s - exiting from JACK", reason);
jack_client_close_aux (client);
/* Need a fix : possibly make client crash if
* zombified without shutdown handler
*/
}
pthread_exit (0);
/*NOTREACHED*/
}
static int
jack_client_process_events (jack_client_t* client)
{
jack_event_t event;
char status = 0;
jack_client_control_t *control = client->control;
JSList *node;
jack_port_t* port;
char* key = 0;
DEBUG ("process events");
if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
DEBUG ("client receives an event, "
"now reading on event fd");
/* server has sent us an event. process the
* event and reply */
if (read_retry (client->event_fd, &event, sizeof(event))
!= sizeof(event)) {
jack_error ("cannot read server event (%s)",
strerror (errno));
return -1;
}
if (event.type == PropertyChange) {
if (event.y.key_size) {
key = (char*)malloc (event.y.key_size);
if (read_retry (client->event_fd, key, event.y.key_size) !=
event.y.key_size) {
jack_error ("cannot read property change key (%s)",
strerror (errno));
return -1;
}
}
}
status = 0;
switch (event.type) {
case PortRegistered:
for (node = client->ports_ext; node; node = jack_slist_next (node)) {
port = node->data;
if (port->shared->id == event.x.port_id) { // Found port, update port type
port->type_info = &client->engine->port_types[port->shared->ptype_id];
}
}
if (control->port_register_cbset) {
client->port_register
(event.x.port_id, TRUE,
client->port_register_arg);
}
break;
case PortUnregistered:
if (control->port_register_cbset) {
client->port_register
(event.x.port_id, FALSE,
client->port_register_arg);
}
break;
case ClientRegistered:
if (control->client_register_cbset) {
client->client_register
(event.x.name, TRUE,
client->client_register_arg);
}
break;
case ClientUnregistered:
if (control->client_register_cbset) {
client->client_register
(event.x.name, FALSE,
client->client_register_arg);
}
break;
case GraphReordered:
status = jack_handle_reorder (client, &event);
break;
case PortConnected:
case PortDisconnected:
status = jack_client_handle_port_connection
(client, &event);
break;
case BufferSizeChange:
jack_client_fix_port_buffers (client);
if (control->bufsize_cbset) {
status = client->bufsize
(client->engine->buffer_size,
client->bufsize_arg);
}
break;
case SampleRateChange:
if (control->srate_cbset) {
status = client->srate
(client->engine->current_time.frame_rate,
client->srate_arg);
}
break;
case XRun:
if (control->xrun_cbset) {
status = client->xrun
(client->xrun_arg);
}
break;
case AttachPortSegment:
jack_attach_port_segment (client, event.y.ptid);
break;
case StartFreewheel:
jack_start_freewheel (client);
break;
case StopFreewheel:
jack_stop_freewheel (client);
break;
case SaveSession:
status = jack_client_handle_session_callback (client, &event );
break;
case LatencyCallback:
status = jack_client_handle_latency_callback (client, &event, 0 );
break;
case PropertyChange:
if (control->property_cbset) {
client->property_cb (event.x.uuid, key, event.z.property_change, client->property_cb_arg);
}
if (key) {
free (key);
}
break;
case PortRename:
if (control->port_rename_cbset) {
client->port_rename_cb (event.y.other_id, event.x.name, event.z.other_name, client->port_rename_arg);
}
break;
}
DEBUG ("client has dealt with the event, writing "
"response on event fd");
if (write_retry (client->event_fd, &status, sizeof(status))
!= sizeof(status)) {
jack_error ("cannot send event response to "
"engine (%s)", strerror (errno));
return -1;
}
}
return 0;
}
static int
jack_wake_next_client (jack_client_t* client)
{
#ifndef JACK_USE_MACH_THREADS
struct pollfd pfds[1];
int pret = 0;
char c = 0;
if (write_retry (client->graph_next_fd, &c, sizeof(c))
!= sizeof(c)) {
DEBUG ("cannot write byte to fd %d", client->graph_next_fd);
jack_error ("cannot continue execution of the "
"processing graph (%s)",
strerror (errno));
return -1;
}
DEBUG ("client sent message to next stage by %" PRIu64 "",
jack_get_microseconds ());
DEBUG ("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
/* "upstream client went away? readability is checked in
* jack_client_core_wait(), but that's almost a whole cycle
* before we get here.
*/
if (client->graph_wait_fd >= 0) {
pfds[0].fd = client->graph_wait_fd;
pfds[0].events = POLLIN;
/* 0 timeout, don't actually wait */
pret = poll (pfds, 1, 0);
}
if (pret > 0 && (pfds[0].revents & POLLIN)) {
if (read_retry (client->graph_wait_fd, &c, sizeof(c))
!= sizeof(c)) {
jack_error ("cannot complete execution of the "
"processing graph (%s)", strerror (errno));
return -1;
}
} else {
DEBUG ("cleanup byte from pipe %d not available?\n",
client->graph_wait_fd);
}
#endif
return 0;
}
#ifdef JACK_USE_MACH_THREADS
static void*
jack_osx_event_thread_work (void* arg)
{
/* this is OS X: this is NOT the process() thread, but instead
just waits for events/callbacks from the server and processes them.
All we do here is to poll() for callbacks from the server,
and then process any callbacks that arrive.
*/
jack_client_t* client = (jack_client_t*)arg;
jack_client_control_t *control = client->control;
if (control->thread_init_cbset) {
DEBUG ("calling OSX event thread init callback");
client->thread_init (client->thread_init_arg);
}
while (1) {
/* this is OS X - we're only waiting on events */
DEBUG ("client polling on %s", client->pollmax == 2 ?
"event_fd and graph_wait_fd..." :
"event_fd only");
while (1) {
if (poll (client->pollfd, client->pollmax, 1000) < 0) {
if (errno == EINTR) {
continue;
}
jack_error ("poll failed in client (%s)",
strerror (errno));
break;
}
pthread_testcancel ();
if (jack_client_process_events (client)) {
DEBUG ("event processing failed\n");
break;
}
}
if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
DEBUG ("client appears dead or event pollfd has error status\n");
break;
}
/* go back and wait for the next one */
}
jack_client_thread_suicide (client, "logic error");
/*NOTREACHED*/
return 0;
}
#else /* !JACK_USE_MACH_THREADS */
static int
jack_client_core_wait (jack_client_t* client)
{
jack_client_control_t *control = client->control;
/* this is not OS X - we're waiting on events & process wakeups */
DEBUG ("client polling on %s", client->pollmax == 2 ?
"event_fd and graph_wait_fd..." :
"event_fd only");
while (1) {
if (poll (client->pollfd, client->pollmax, 1000) < 0) {
if (errno == EINTR) {
continue;
}
jack_error ("poll failed in client (%s)",
strerror (errno));
return -1;
}
pthread_testcancel ();
/* get an accurate timestamp on waking from poll for a
* process() cycle.
*/
if (client->graph_wait_fd >= 0
&& client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
control->awake_at = jack_get_microseconds ();
}
DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
client->pollfd[EVENT_POLL_INDEX].revents,
client->pollfd[WAIT_POLL_INDEX].revents);
if (client->graph_wait_fd >= 0 &&
(client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
/* our upstream "wait" connection
closed, which either means that
an intermediate client exited, or
jackd exited, or jackd zombified
us.
we can discover the zombification
via client->control->dead, but
the other two possibilities are
impossible to identify just from
this situation. so we have to
check what we are connected to,
and act accordingly.
*/
if (client->upstream_is_jackd) {
DEBUG ("WE (%s) DIE\n", client->name);
return 0;
} else {
DEBUG ("WE PUNT\n");
/* don't poll on the wait fd
* again until we get a
* GraphReordered event.
*/
client->graph_wait_fd = -1;
client->pollmax = 1;
}
}
if (jack_client_process_events (client)) {
DEBUG ("event processing failed\n");
return 0;
}
if (client->graph_wait_fd >= 0 &&
(client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
DEBUG ("time to run process()\n");
break;
}
}
if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
DEBUG ("client appears dead or event pollfd has error status\n");
return -1;
}
return 0;
}
#endif
static void*
jack_process_thread_work (void* arg)
{
/* this is the RT process thread used to handle process()
callbacks, and on non-OSX systems, server events/callbacks
as well.
*/
jack_client_t* client = (jack_client_t*)arg;
jack_client_control_t *control = client->control;
/* notify the waiting client that this thread
is up and running.
*/
pthread_mutex_lock (&client_lock);
client->thread_ok = TRUE;
client->thread_id = pthread_self ();
pthread_cond_signal (&client_ready);
pthread_mutex_unlock (&client_lock);
control->pid = getpid ();
control->pgrp = getpgrp ();
#ifdef JACK_USE_MACH_THREADS
client->rt_thread_ok = TRUE;
#endif
if (control->thread_cb_cbset) {
/* client provided a thread function to run,
so just do that.
*/
client->thread_cb (client->thread_cb_arg);
} else {
if (control->thread_init_cbset) {
DEBUG ("calling process thread init callback");
client->thread_init (client->thread_init_arg);
}
while (1) {
int status;
if (jack_cycle_wait (client) != client->engine->buffer_size) {
break;
}
if (control->process_cbset) {
/* run process callback, then wait... ad-infinitum */
DEBUG ("client calls process()");
status = client->process (client->engine->buffer_size, client->process_arg);
control->state = Finished;
} else {
status = 0;
}
/* if status was non-zero, this will not return (it will call
jack_client_thread_suicide()
*/
jack_cycle_signal (client, status);
}
}
jack_client_thread_suicide (client, "logic error");
/*NOTREACHED*/
return 0;
}
jack_nframes_t
jack_thread_wait (jack_client_t* client, int status)
{
static int msg_delivered = 0;
if (!msg_delivered) {
jack_error ("jack_thread_wait(): deprecated, use jack_cycle_wait/jack_cycle_signal");
msg_delivered = 1;
}
return 0;
}
jack_nframes_t jack_cycle_wait (jack_client_t* client)
{
jack_client_control_t *control = client->control;
/* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
#ifdef JACK_USE_MACH_THREADS
/* on OS X systems, this thread is running a callback provided
by the client that has called this function in order to wait
for the next process callback. This is how we do that ...
*/
jack_client_suspend (client);
#else
/* on non-OSX systems, this thread is running a callback provided
by the client that has called this function in order to wait
for the next process() callback or the next event from the
server.
*/
if (jack_client_core_wait (client)) {
return 0;
}
#endif
/* SECTION THREE: START NEXT DATA PROCESSING TIME */
/* Time to do data processing */
control->awake_at = jack_get_microseconds ();
client->control->state = Running;
/* begin preemption checking */
CHECK_PREEMPTION (client->engine, TRUE);
if (client->control->sync_cb_cbset) {
jack_call_sync_client (client);
}
return client->engine->buffer_size;
}
void jack_cycle_signal (jack_client_t* client, int status)
{
client->control->last_status = status;
/* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
/* housekeeping/cleanup after data processing */
if (status == 0 && client->control->timebase_cb_cbset) {
jack_call_timebase_master (client);
}
/* end preemption checking */
CHECK_PREEMPTION (client->engine, FALSE);
client->control->finished_at = jack_get_microseconds ();
client->control->state = Finished;
/* wake the next client in the chain (could be the server),
and check if we were killed during the process
cycle.
*/
if (jack_wake_next_client (client)) {
DEBUG ("client cannot wake next, or is dead\n");
jack_client_thread_suicide (client, "graph error");
/*NOTREACHED*/
}
if (client->control->dead) {
jack_client_thread_suicide (client, "zombified");
/*NOTREACHED*/
}
if (status) {
jack_client_thread_suicide (client, "process error");
/*NOTREACHED*/
}
if (!client->engine->engine_ok) {
jack_client_thread_suicide (client, "JACK died");
/*NOTREACHED*/
}
}
static int
jack_start_thread (jack_client_t *client)
{
#ifdef USE_MLOCK
if (client->engine->real_time) {
if (client->engine->do_mlock
&& (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
jack_error ("cannot lock down memory for RT thread "
"(%s)", strerror (errno));
}
if (client->engine->do_munlock) {
cleanup_mlock ();
}
}
#endif /* USE_MLOCK */
#ifdef JACK_USE_MACH_THREADS
/* Stephane Letz : letz@grame.fr
On MacOSX, the event/callback-handling thread does not need to be real-time.
*/
if (jack_client_create_thread (client,
&client->thread,
client->engine->client_priority,
FALSE,
jack_osx_event_thread_work, client)) {
return -1;
}
#else
if (jack_client_create_thread (client,
&client->thread,
client->engine->client_priority,
client->engine->real_time,
jack_process_thread_work, client)) {
return -1;
}
#endif
#ifdef JACK_USE_MACH_THREADS
/* a secondary thread that runs the process callback and uses
ultra-fast Mach primitives for inter-thread signalling.
XXX in a properly structured JACK, there would be no
need for this, because we would have client wake up
methods that encapsulated the underlying mechanism
used.
*/
if (jack_client_create_thread (client,
&client->process_thread,
client->engine->client_priority,
client->engine->real_time,
jack_process_thread_work, client)) {
return -1;
}
#endif /* JACK_USE_MACH_THREADS */
return 0;
}
int
jack_activate (jack_client_t *client)
{
jack_request_t req;
VALGRIND_MEMSET (&req, 0, sizeof(req));
if (client->control->type == ClientInternal ||
client->control->type == ClientDriver) {
goto startit;
}
/* get the pid of the client process to pass it to engine */
client->control->pid = getpid ();
#ifdef USE_CAPABILITIES
if (client->engine->has_capabilities != 0 &&
client->control->pid != 0 && client->engine->real_time != 0) {
/* we need to ask the engine for realtime capabilities
before trying to start the realtime thread
*/
req.type = SetClientCapabilities;
req.x.client_id = client->control->id;
req.x.cap_pid = client->control->pid;
jack_client_deliver_request (client, &req);
if (req.status) {
/* what to do? engine is running realtime, it
is using capabilities and has them
(otherwise we would not get an error
return) but for some reason it could not
give the client the required capabilities.
For now, leave the client so that it
still runs, albeit non-realtime.
*/
jack_error ("could not receive realtime capabilities, "
"client will run non-realtime");
}
}
#endif /* USE_CAPABILITIES */
if (client->first_active) {
pthread_mutex_init (&client_lock, NULL);
pthread_cond_init (&client_ready, NULL);
pthread_mutex_lock (&client_lock);
if (jack_start_thread (client)) {
pthread_mutex_unlock (&client_lock);
return -1;
}
pthread_cond_wait (&client_ready, &client_lock);
pthread_mutex_unlock (&client_lock);
if (!client->thread_ok) {
jack_error ("could not start client thread");
return -1;
}
client->first_active = FALSE;
}
startit:
req.type = ActivateClient;
jack_uuid_copy (&req.x.client_id, client->control->uuid);
return jack_client_deliver_request (client, &req);
}
static int
jack_deactivate_aux (jack_client_t *client)
{
jack_request_t req;
int rc = ESRCH; /* already shut down */
if (client && client->control) { /* not shut down? */
rc = 0;
if (client->control->active) { /* still active? */
VALGRIND_MEMSET (&req, 0, sizeof(req));
req.type = DeactivateClient;
jack_uuid_copy (&req.x.client_id, client->control->uuid);
rc = jack_client_deliver_request (client, &req);
}
}
return rc;
}
int
jack_deactivate (jack_client_t *client)
{
return jack_deactivate_aux (client);
}
static int
jack_client_close_aux (jack_client_t *client)
{
JSList *node;
void *status;
int rc;
rc = jack_deactivate_aux (client);
if (rc == ESRCH) { /* already shut down? */
return rc;
}
if (client->control->type == ClientExternal) {
#if JACK_USE_MACH_THREADS
if (client->rt_thread_ok) {
// MacOSX pthread_cancel not implemented in
// Darwin 5.5, 6.4
mach_port_t machThread =
pthread_mach_thread_np (client->process_thread);
thread_terminate (machThread);
}
#endif
/* stop the thread that communicates with the jack
* server, only if it was actually running
*/
if (client->thread_ok) {
pthread_cancel (client->thread);
pthread_join (client->thread, &status);
}
if (client->control) {
jack_release_shm (&client->control_shm);
client->control = NULL;
}
if (client->engine) {
jack_release_shm (&client->engine_shm);
client->engine = NULL;
}
if (client->port_segment) {
jack_port_type_id_t ptid;
for (ptid = 0; ptid < client->n_port_types; ++ptid)
jack_release_shm (&client->port_segment[ptid]);
free (client->port_segment);
client->port_segment = NULL;
}
#ifndef JACK_USE_MACH_THREADS
if (client->graph_wait_fd >= 0) {
close (client->graph_wait_fd);
}
if (client->graph_next_fd >= 0) {
close (client->graph_next_fd);
}
#endif
close (client->event_fd);
if (shutdown (client->request_fd, SHUT_RDWR)) {
jack_error ("could not shutdown client request socket");
}
close (client->request_fd);
}
for (node = client->ports; node; node = jack_slist_next (node))
free (node->data);
jack_slist_free (client->ports);
for (node = client->ports_ext; node; node = jack_slist_next (node))
free (node->data);
jack_slist_free (client->ports_ext);
jack_client_free (client);
jack_messagebuffer_exit ();
return rc;
}
int
jack_client_close (jack_client_t *client)
{
return jack_client_close_aux (client);
}
int
jack_is_realtime (jack_client_t *client)
{
return client->engine->real_time;
}
jack_nframes_t
jack_get_buffer_size (jack_client_t *client)
{
return client->engine->buffer_size;
}
int
jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
{
#ifdef DO_BUFFER_RESIZE
jack_request_t req;
VALGRIND_MEMSET (&req, 0, sizeof(req));
if (nframes < 1 || nframes > 16384) {
return ERANGE;
}
req.type = SetBufferSize;
req.x.nframes = nframes;
return jack_client_deliver_request (client, &req);
#else
return ENOSYS;
#endif /* DO_BUFFER_RESIZE */
}
int
jack_connect (jack_client_t *client, const char *source_port,
const char *destination_port)
{
jack_request_t req;
VALGRIND_MEMSET (&req, 0, sizeof(req));
req.type = ConnectPorts;
snprintf (req.x.connect.source_port,
sizeof(req.x.connect.source_port), "%s", source_port);
snprintf (req.x.connect.destination_port,
sizeof(req.x.connect.destination_port),
"%s", destination_port);
return jack_client_deliver_request (client, &req);
}
int
jack_port_disconnect (jack_client_t *client, jack_port_t *port)
{
jack_request_t req;
if (port->shared->client_id == client->control->uuid) {
pthread_mutex_lock (&port->connection_lock);
if (port->connections == NULL) {
printf ("JACK port not connected\n");
pthread_mutex_unlock (&port->connection_lock);
return 0;
}
pthread_mutex_unlock (&port->connection_lock);
}
VALGRIND_MEMSET (&req, 0, sizeof(req));
req.type = DisconnectPort;
req.x.port_info.port_id = port->shared->id;
return jack_client_deliver_request (client, &req);
}
int
jack_disconnect (jack_client_t *client, const char *source_port,
const char *destination_port)
{
jack_request_t req;
VALGRIND_MEMSET (&req, 0, sizeof(req));
req.type = DisconnectPorts;
snprintf (req.x.connect.source_port,
sizeof(req.x.connect.source_port), "%s", source_port);
snprintf (req.x.connect.destination_port,
sizeof(req.x.connect.destination_port),
"%s", destination_port);
return jack_client_deliver_request (client, &req);
}
void
jack_set_error_function (void (*func)(const char *))
{
jack_error_callback = func;
}
void
jack_set_info_function (void (*func)(const char *))
{
jack_info_callback = func;
}
int
jack_set_graph_order_callback (jack_client_t *client,
JackGraphOrderCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->graph_order = callback;
client->graph_order_arg = arg;
client->control->graph_order_cbset = (callback != NULL);
return 0;
}
int
jack_set_latency_callback (jack_client_t *client,
JackLatencyCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->latency_cb = callback;
client->latency_cb_arg = arg;
client->control->latency_cbset = (callback != NULL);
return 0;
}
int jack_set_xrun_callback (jack_client_t *client,
JackXRunCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->xrun = callback;
client->xrun_arg = arg;
client->control->xrun_cbset = (callback != NULL);
return 0;
}
int
jack_set_process_callback (jack_client_t *client,
JackProcessCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
if (client->control->thread_cb_cbset) {
jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
return -1;
}
client->process_arg = arg;
client->process = callback;
client->control->process_cbset = (callback != NULL);
return 0;
}
int
jack_set_thread_init_callback (jack_client_t *client,
JackThreadInitCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->thread_init_arg = arg;
client->thread_init = callback;
client->control->thread_init_cbset = (callback != NULL);
/* make sure that the message buffer thread is initialized too */
jack_messagebuffer_thread_init (callback, arg);
return 0;
}
int
jack_set_freewheel_callback (jack_client_t *client,
JackFreewheelCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->freewheel_arg = arg;
client->freewheel_cb = callback;
client->control->freewheel_cb_cbset = (callback != NULL);
return 0;
}
int
jack_set_buffer_size_callback (jack_client_t *client,
JackBufferSizeCallback callback, void *arg)
{
client->bufsize_arg = arg;
client->bufsize = callback;
client->control->bufsize_cbset = (callback != NULL);
return 0;
}
int
jack_set_port_rename_callback (jack_client_t *client,
JackPortRenameCallback callback,
void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->port_rename_arg = arg;
client->port_rename_cb = callback;
client->control->port_rename_cbset = (callback != NULL);
return 0;
}
int
jack_set_port_registration_callback (jack_client_t *client,
JackPortRegistrationCallback callback,
void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->port_register_arg = arg;
client->port_register = callback;
client->control->port_register_cbset = (callback != NULL);
return 0;
}
int
jack_set_port_connect_callback (jack_client_t *client,
JackPortConnectCallback callback,
void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->port_connect_arg = arg;
client->port_connect = callback;
client->control->port_connect_cbset = (callback != NULL);
return 0;
}
int
jack_set_client_registration_callback (jack_client_t *client,
JackClientRegistrationCallback callback,
void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->client_register_arg = arg;
client->client_register = callback;
client->control->client_register_cbset = (callback != NULL);
return 0;
}
int
jack_set_process_thread (jack_client_t* client, JackThreadCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
if (client->control->process_cbset) {
jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
return -1;
}
client->thread_cb_arg = arg;
client->thread_cb = callback;
client->control->thread_cb_cbset = (callback != NULL);
return 0;
}
int
jack_set_session_callback (jack_client_t* client, JackSessionCallback callback, void *arg)
{
if (client->control->active) {
jack_error ("You cannot set callbacks on an active client.");
return -1;
}
client->session_cb_arg = arg;
client->session_cb = callback;
client->control->session_cbset = (callback != NULL);
return 0;
}
int
jack_get_process_done_fd (jack_client_t *client)
{
return client->graph_next_fd;
}
void
jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
{
client->on_shutdown = function;
client->on_shutdown_arg = arg;
}
void
jack_on_info_shutdown (jack_client_t *client, void (*function)(jack_status_t, const char*, void *arg), void *arg)
{
client->on_info_shutdown = function;
client->on_info_shutdown_arg = arg;
}
char *
jack_get_client_name_by_uuid (jack_client_t *client, const char *uuid_str)
{
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
if (jack_uuid_parse (uuid_str, &request.x.client_id) != 0) {
return NULL;
}
request.type = GetClientByUUID;
if ( jack_client_deliver_request (client, &request)) {
return NULL;
}
return strdup (request.x.port_info.name);
}
char*
jack_get_uuid_for_client_name (jack_client_t *client, const char *client_name)
{
jack_request_t request;
size_t len = strlen (client_name) + 1;
if ( len > sizeof(request.x.name) ) {
return NULL;
}
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = GetUUIDByClientName;
memcpy (request.x.name, client_name, len);
if (jack_client_deliver_request ( client, &request)) {
return NULL;
}
char buf[37];
jack_uuid_unparse (request.x.client_id, buf);
return strdup (buf);
}
char *
jack_client_get_uuid (jack_client_t *client)
{
char retval[37];
jack_uuid_unparse (client->control->uuid, retval);
return strdup (retval);
}
int
jack_reserve_client_name (jack_client_t *client, const char *name, const char *uuid_str)
{
jack_request_t request;
VALGRIND_MEMSET (&request, 0, sizeof(request));
request.type = ReserveName;
snprintf ( request.x.reservename.name, sizeof( request.x.reservename.name ), "%s", name );
if (jack_uuid_parse (uuid_str, &request.x.reservename.uuid) != 0) {
return -1;
}
return jack_client_deliver_request (client, &request);
}
const char **
jack_get_ports (jack_client_t *client,
const char *port_name_pattern,
const char *type_name_pattern,
unsigned long flags)
{
jack_control_t *engine;
const char **matching_ports;
unsigned long match_cnt;
jack_port_shared_t *psp;
unsigned long i;
regex_t port_regex;
regex_t type_regex;
int matching;
engine = client->engine;
if (port_name_pattern && port_name_pattern[0]) {
regcomp (&port_regex, port_name_pattern,
REG_EXTENDED | REG_NOSUB);
}
if (type_name_pattern && type_name_pattern[0]) {
regcomp (&type_regex, type_name_pattern,
REG_EXTENDED | REG_NOSUB);
}
psp = engine->ports;
match_cnt = 0;
if ((matching_ports = (const char**)malloc (sizeof(char *) * (engine->port_max + 1))) == NULL) {
return NULL;
}
for (i = 0; i < engine->port_max; i++) {
matching = 1;
if (!psp[i].in_use) {
continue;
}
if (flags) {
if ((psp[i].flags & flags) != flags) {
matching = 0;
}
}
if (matching && port_name_pattern && port_name_pattern[0]) {
if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
matching = 0;
}
}
if (matching && type_name_pattern && type_name_pattern[0]) {
jack_port_type_id_t ptid = psp[i].ptype_id;
if (regexec (&type_regex,
engine->port_types[ptid].type_name,
0, NULL, 0)) {
matching = 0;
}
}
if (matching) {
matching_ports[match_cnt++] = psp[i].name;
}
}
if (port_name_pattern && port_name_pattern[0]) {
regfree (&port_regex);
}
if (type_name_pattern && type_name_pattern[0]) {
regfree (&type_regex);
}
if (match_cnt == 0) {
free (matching_ports);
matching_ports = 0;
} else {
matching_ports[match_cnt] = 0;
}
return matching_ports;
}
float
jack_cpu_load (jack_client_t *client)
{
return client->engine->cpu_load;
}
float
jack_get_xrun_delayed_usecs (jack_client_t *client)
{
return client->engine->xrun_delayed_usecs;
}
float
jack_get_max_delayed_usecs (jack_client_t *client)
{
return client->engine->max_delayed_usecs;
}
void
jack_reset_max_delayed_usecs (jack_client_t *client)
{
client->engine->max_delayed_usecs = 0.0f;
}
pthread_t
jack_client_thread_id (jack_client_t *client)
{
if (client->control->type != ClientExternal) {
/* Internal and driver clients run in ... ??? */
return 0;
}
return client->thread_id;
}
int
jack_client_name_size (void)
{
return JACK_CLIENT_NAME_SIZE;
}
int
jack_port_name_size (void)
{
return JACK_PORT_NAME_SIZE;
}
int
jack_port_type_size (void)
{
return JACK_PORT_TYPE_SIZE;
}
void
jack_free (void* ptr)
{
free (ptr);
}
const char*
jack_event_type_name (JackEventType type)
{
switch (type) {
case BufferSizeChange:
return "buffer size change";
case SampleRateChange:
return "sample rate change";
case AttachPortSegment:
return "port segment attached";
case PortConnected:
return "ports connected";
case PortDisconnected:
return "ports disconnected";
case GraphReordered:
return "graph reordered";
case PortRegistered:
return "port registered";
case PortUnregistered:
return "port unregistered";
case XRun:
return "xrun";
case StartFreewheel:
return "freewheel started";
case StopFreewheel:
return "freewheel stopped";
case ClientRegistered:
return "client registered";
case ClientUnregistered:
return "client unregistered";
case SaveSession:
return "save session";
case LatencyCallback:
return "latency callback";
case PropertyChange:
return "property change callback";
case PortRename:
return "port rename";
default:
break;
}
return "unknown";
}
|