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
|
/*
MIDI Sequencer C++ library
Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCoreApplication>
#include <QFile>
#include <QMetaMethod>
#include <QReadLocker>
#include <QRegularExpression>
#include <QThread>
#include <QWriteLocker>
#include <drumstick/alsaclient.h>
#include <drumstick/alsaevent.h>
#include <drumstick/alsaqueue.h>
#include "errorcheck.h"
#if defined(RTKIT_SUPPORT)
#include <QDBusConnection>
#include <QDBusInterface>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/types.h>
#endif
#include <pthread.h>
#ifndef RLIMIT_RTTIME
#define RLIMIT_RTTIME 15
#endif
#ifndef SCHED_RESET_ON_FORK
#define SCHED_RESET_ON_FORK 0x40000000
#endif
#ifndef DEFAULT_INPUT_TIMEOUT
#define DEFAULT_INPUT_TIMEOUT 500
#endif
/**
* @file alsaclient.cpp
* Implementation of classes managing ALSA Sequencer clients
*/
/**
* @class QObject
* The QObject class is the base class of all Qt objects.
* @see https://doc.qt.io/qt-5/qobject.html
*/
/**
* @class QThread
* The QThread class provides platform-independent threads.
* @see https://doc.qt.io/qt-5/qthread.html
*/
namespace drumstick { namespace ALSA {
/**
* @addtogroup ALSAClient
* @{
*
* ALSA clients are any entities using ALSA sequencer services. A client
* may be an application or a device driver for an external MIDI port, like
* USB MIDI devices or the MIDI/game ports of some sound cards. This library
* allows to easily create applications managing ALSA clients.
*
* ALSA clients are also file descriptors representing a sequencer device,
* that must be opened before reading or writing MIDI events. When the client
* is opened, it is given some handle and a number identifying it to other
* clients in the system. You can also provide a name for it.
*
* Each ALSA sequencer client can have several ports attached. The ports can be
* readable or writable, and can be subscribed in pairs: one readable port to
* one writable port. The subscriptions can be made and queried by external
* applications, like "aconnect" or "qjackctl".
*
* SystemInfo is an auxiliary class to query several system capabilities.
*
* The PoolInfo class represents a container to query and change some values
* for the kernel memory pool assigned to an ALSA client.
*
* The ClientInfo class is another container to query and change properties of
* the MidiClient itself.
*
* The SequencerEventHandler abstract class is used to define an interface
* that other class can implement to receive sequencer events. It is one of the
* three methods of delivering input events offered by the library.
*
* @section EventInput Input
* MidiClient uses a separate thread to receive events from the ALSA sequencer.
* The input thread can be started and stopped using the methods
* MidiClient::startSequencerInput() and MidiClient::stopSequencerInput().
* It is necessary to have this thread in mind when using this library to read
* events. There are three delivering methods of input events:
* <ul>
* <li>A Callback method. To use this method, you must derive a class from
* SequencerEventHandler, overriding the method
* SequencerEventHandler::handleSequencerEvent() to provide your own event
* processing code. You must give a handler instance pointer to
* the client using MidiClient::setHandler().</li>
* <li>Using QEvent listeners. To use this method, you must have one or more
* classes derived from QObject overriding the method QObject::customEvent().
* You must also use the method MidiClient::addListener() to add such objects
* to the client's listeners list, and MidiClient::setEventsEnabled().</li>
* <li>The third method involves signals and slots. Whenever a sequencer event
* is received, a signal MidiClient::eventReceived() is emitted, that can be
* connected to your own supplied slot(s) to process it.
* </ul>
* The selected method depends only on your requirements and your preferences.
* <ul>
* <li>The Callback method is preferred for real-time usage because the handler
* receives the events without any delay, but at the same time you must
* avoid calling methods of any GUI widgets within the handler. Instead,
* you can create QEvents and call QObject::postEvent() to notify the GUI.</li>
* <li>Inside QObject::eventReceiver() you can collect QEvents and call
* any method you want, but the events are not delivered in real-time. Instead,
* they are enqueued and dispatched by the main application's event loop.</li>
* <li>The signals/slots method can be real-time or queued, depending on the
* last parameter of QObject::connect(). If it is Qt::DirectConnection, the signal
* is delivered in real-time, and the same rule about avoiding calls to any
* GUI widgets methods apply. If it is Qt::QueuedConnection, then the signal is
* enqueued using the application's event loop, and it is safe to call any GUI
* methods in this case.</li>
* </ul>
* Whichever method you select, it excludes the other methods for the same
* program. A callback takes precedence over the others. If it is not set, then
* the events are sent if MidiClient::setEventsEnabled() is called.
* If neither a callback handler is set nor events are enabled, then the signal
* is emitted. In any case, the event pointer must be deleted by the receiver
* method.
*
* @see https://doc.qt.io/qt-5/threads-reentrancy.html
*
* @section EventOutput Output
*
* The methods to send a single event to the ALSA sequencer are:
* <ul>
* <li>MidiClient::output() using the library buffer, automatically flushed.</li>
* <li>MidiClient::outputBuffer() using the library buffer. Not flushed automatically.</li>
* <li>MidiClient::outputDirect() not using the library buffer.</li>
* </ul>
* The two first methods usually require a call to MidiClient::drainOutput() to
* flush the ALSA library output buffer. The third one bypasses the buffer, and
* doesn't require the call to MidiClient::drainOutput(). Note that the buffer
* can be automatically drained by the first method when it becomes full.
*
* After being dispatched to the ALSA Sequencer, the events can be scheduled at
* some time in the future, or immediately. This depends on the following
* methods of the SequencerEvent class:
* <ul>
* <li>SequencerEvent::setDirect() not scheduled</li>
* <li>SequencerEvent::scheduleTick() scheduled in musical time (ticks)</li>
* <li>SequencerEvent::scheduleReal() scheduled in clock time (seconds)</li>
* </ul>
*
* When you need to schedule a lot of events, for instance reproducing
* a Standard MIDI File (SMF) or a MIDI sequence, you may want to use the
* abstract class SequencerOutputThread.
*
* @section Memory
*
* There are two memory issues: the memory pool belongs to the kernel sequencer,
* and can be managed by the class PoolInfo and the methods
* MidiClient::getPoolInfo() and MidiClient::setPoolInfo(). The library buffer
* can be controlled using the methods MidiClient::getOutputBufferSize() and
* MidiClient::setOutputBufferSize() as well as MidiClient::getInputBufferSize()
* and MidiClient::setInputBufferSize().
*
* @see https://www.alsa-project.org/alsa-doc/alsa-lib/group___seq_client.html
*/
/**
* This class manages event input from the ALSA sequencer.
*/
class MidiClient::SequencerInputThread: public QThread
{
public:
SequencerInputThread(MidiClient *seq, int timeout)
: QThread(),
m_MidiClient(seq),
m_Wait(timeout),
m_Stopped(false),
m_RealTime(true) {}
virtual ~SequencerInputThread() = default;
void run() override;
bool stopped();
void stop();
void setRealtimePriority();
MidiClient *m_MidiClient;
int m_Wait;
bool m_Stopped;
bool m_RealTime;
QReadWriteLock m_mutex;
};
class MidiClient::MidiClientPrivate
{
public:
MidiClientPrivate() :
m_eventsEnabled(false),
m_BlockMode(false),
m_NeedRefreshClientList(true),
m_OpenMode(SND_SEQ_OPEN_DUPLEX),
m_DeviceName("default"),
m_SeqHandle(nullptr),
m_Thread(nullptr),
m_Queue(nullptr),
m_handler(nullptr)
{ }
bool m_eventsEnabled;
bool m_BlockMode;
bool m_NeedRefreshClientList;
int m_OpenMode;
QString m_DeviceName;
snd_seq_t* m_SeqHandle;
QPointer<SequencerInputThread> m_Thread;
QPointer<MidiQueue> m_Queue;
SequencerEventHandler* m_handler;
ClientInfo m_Info;
ClientInfoList m_ClientList;
MidiPortList m_Ports;
PortInfoList m_OutputsAvail;
PortInfoList m_InputsAvail;
QObjectList m_listeners;
SystemInfo m_sysInfo;
PoolInfo m_poolInfo;
};
/**
* Constructor.
*
* This constructor optionally gets a QObject parent. When you create a
* MidiClient with another object as parent, the MidiClient object will
* automatically add itself to the parent's children() list. The parent takes
* ownership of the object i.e. it will automatically delete its children in
* its destructor.
*
* It is necessary to invoke open() later to get the sequencer client handler
* from the ALSA sequencer subsystem.
*
* @param parent The optional parent object
*/
MidiClient::MidiClient( QObject* parent ) :
QObject(parent),
d(new MidiClientPrivate)
{
qRegisterMetaType<drumstick::ALSA::SequencerEvent>();
qRegisterMetaType<drumstick::ALSA::SequencerEvent*>();
}
/**
* Destructor.
*
* The ports and queue associated to this client are automatically released.
*/
MidiClient::~MidiClient()
{
stopSequencerInput();
detachAllPorts();
delete d->m_Queue;
close();
freeClients();
delete d->m_Thread;
}
/**
* Returns the sequencer handler managed by ALSA
* @return the sequencer handler
*/
snd_seq_t*
MidiClient::getHandle()
{
return d->m_SeqHandle;
}
/**
* Returns true if the sequencer is opened
* @return wheter the sequencer is opened
*/
bool MidiClient::isOpened()
{
return !d.isNull() && (d->m_SeqHandle != nullptr);
}
/**
* Returns the name of the sequencer device
* @return the device name
*/
QString MidiClient::getDeviceName()
{
return d->m_DeviceName;
}
/**
* Returns the last open mode used in open()
* @return the last open mode
*/
int MidiClient::getOpenMode()
{
return d->m_OpenMode;
}
/**
* Returns the last block mode used in open()
* @return the last block mode
*/
bool MidiClient::getBlockMode()
{
return d->m_BlockMode;
}
/**
* Returns true if the events mode of delivery has been enabled
* @return whether the events mode of delivery is enabled
*/
bool MidiClient::getEventsEnabled() const
{
return d->m_eventsEnabled;
}
/**
* Sets a sequencer event handler enabling the callback delivery mode
* @param handler the sequencer event handler
*/
void MidiClient::setHandler(SequencerEventHandler* handler)
{
d->m_handler = handler;
}
/**
* Enables real-time priority for the MIDI input thread. The system needs either
* RLIMIT_RTPRIO or RealtimeKit. First RLIMIT_RTPRIO is tried, and if this
* method fails, RealtimeKit is used.
*
* @param enable real-time priority enabled
* @since 0.5.0
*/
void MidiClient::setRealTimeInput(bool enable)
{
if (d->m_Thread == nullptr) {
d->m_Thread = new SequencerInputThread(this, DEFAULT_INPUT_TIMEOUT);
d->m_Thread->m_RealTime = enable;
}
}
/**
* Return the real-time priority setting for the MIDI input thread.
* @return true if the real-time priority is enabled
* @since 0.5.0
*/
bool MidiClient::realTimeInputEnabled()
{
if (d->m_Thread == nullptr)
return true;
return d->m_Thread->m_RealTime;
}
/**
* Open the sequencer device.
*
* When opening the MidiClient instance, several properties may optionally
* be set as the device name, the open mode and block mode. Default values
* are provided for them. After a successful open, an event with
* SND_SEQ_EVENT_CLIENT_START is broadcast to the announce port.
*
* @param deviceName the sequencer device name, default value = "default".
* This is not a name you make up for your own purposes; it has special
* significance to the ALSA library. Usually you need to pass "default" here.
* @param openMode the open mode, default value = SND_SEQ_OPEN_DUPLEX.
* The read/write mode of the sequencer. Can be one of these three values:
* <ul>
* <li>SND_SEQ_OPEN_OUTPUT - open the sequencer for output only</li>
* <li>SND_SEQ_OPEN_INPUT - open the sequencer for input only</li>
* <li>SND_SEQ_OPEN_DUPLEX - open the sequencer for output and input</li>
* </ul>
* @param blockMode open in blocking mode, default value = false.
*/
void
MidiClient::open( const QString deviceName,
const int openMode,
const bool blockMode)
{
DRUMSTICK_ALSA_CHECK_ERROR( snd_seq_open( &d->m_SeqHandle, deviceName.toLocal8Bit().data(),
openMode, blockMode ? 0 : SND_SEQ_NONBLOCK ) );
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_get_client_info( d->m_SeqHandle, d->m_Info.m_Info ) );
d->m_DeviceName = deviceName;
d->m_OpenMode = openMode;
d->m_BlockMode = blockMode;
}
/**
* Open the sequencer device, providing a configuration object pointer.
*
* This method is like open() except that a configuration is explicitly
* provided. After a successful open, an event with SND_SEQ_EVENT_CLIENT_START
* type is broadcasted from the announce port.
*
* @param conf a configuration object pointer.
* @param deviceName the sequencer device name, default value = "default".
* This is not a name you make up for your own purposes; it has special
* significance to the ALSA library. Usually you need to pass "default" here.
* @param openMode the open mode, default value = SND_SEQ_OPEN_DUPLEX.
* The read/write mode of the sequencer. Can be one of these three values:
* <ul>
* <li>SND_SEQ_OPEN_OUTPUT - open the sequencer for output only</li>
* <li>SND_SEQ_OPEN_INPUT - open the sequencer for input only</li>
* <li>SND_SEQ_OPEN_DUPLEX - open the sequencer for output and input</li>
* </ul>
* @param blockMode open in blocking mode, default value = false.
*/
void
MidiClient::open( snd_config_t* conf,
const QString deviceName,
const int openMode,
const bool blockMode )
{
DRUMSTICK_ALSA_CHECK_ERROR( snd_seq_open_lconf( &d->m_SeqHandle,
deviceName.toLocal8Bit().data(),
openMode,
blockMode ? 0 : SND_SEQ_NONBLOCK,
conf ));
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_get_client_info(d->m_SeqHandle, d->m_Info.m_Info));
d->m_DeviceName = deviceName;
d->m_OpenMode = openMode;
d->m_BlockMode = blockMode;
}
/**
* Close the sequencer device.
*
* After a client is closed, an event with SND_SEQ_EVENT_CLIENT_EXIT is
* broadcast to the announce port. The connection between other clients are
* disconnected. Call this just before exiting your program.
*/
void
MidiClient::close()
{
if (d->m_SeqHandle != nullptr) {
stopSequencerInput();
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_close(d->m_SeqHandle));
d->m_SeqHandle = nullptr;
}
}
/**
* Gets the size of the library output buffer for the ALSA client.
*
* This buffer is used to store the decoded byte-stream of output events before
* transferring to the sequencer.
*
* @return the size of the library output buffer
*/
size_t
MidiClient::getOutputBufferSize()
{
return snd_seq_get_output_buffer_size(d->m_SeqHandle);
}
/**
* Sets the size of the library output buffer for the ALSA client.
*
* This buffer is used to store the decoded byte-stream of output events before
* transferring to the sequencer.
*
* @param newSize the size of the library output buffer
*/
void
MidiClient::setOutputBufferSize(size_t newSize)
{
if (getOutputBufferSize() != newSize) {
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_output_buffer_size(d->m_SeqHandle, newSize));
}
}
/**
* Gets the size of the library input buffer for the ALSA client.
*
* This buffer is used to read a byte-stream of input events before
* transferring from the sequencer.
*
* @return the size of the library input buffer
*/
size_t
MidiClient::getInputBufferSize()
{
return snd_seq_get_input_buffer_size(d->m_SeqHandle);
}
/**
* Sets the size of the library input buffer for the ALSA client.
*
* This buffer is used to read a byte-stream of input events before
* transferring from the sequencer.
*
* @param newSize the size of the library input buffer
*/
void
MidiClient::setInputBufferSize(size_t newSize)
{
if (getInputBufferSize() != newSize) {
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_input_buffer_size(d->m_SeqHandle, newSize));
}
}
/**
* Change the blocking mode of the client.
*
* In block mode, the client falls into sleep when it fills the output memory
* pool with full events. The client will be woken up after a certain amount
* of free space becomes available.
*
* @param newValue the blocking mode
*/
void
MidiClient::setBlockMode(bool newValue)
{
if (d->m_BlockMode != newValue)
{
d->m_BlockMode = newValue;
if (d->m_SeqHandle != nullptr)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_nonblock(d->m_SeqHandle, d->m_BlockMode ? 0 : 1));
}
}
}
/**
* Gets the client ID.
*
* Returns the ID of the client. A client ID is necessary to inquiry or to set
* the client information. A user client ID is assigned from 128 to 191.
*
* @return the client ID.
*/
int
MidiClient::getClientId()
{
return DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_client_id(d->m_SeqHandle));
}
/**
* Returns the type snd_seq_type_t of the given sequencer handle.
* @return the type snd_seq_type_t of the given sequencer handle.
*/
snd_seq_type_t
MidiClient::getSequencerType()
{
return snd_seq_type(d->m_SeqHandle);
}
/**
* Dispatch the events received from the Sequencer.
*
* There are three methods of events delivering:
* <ul>
* <li>A Callback method. To use this method, you must derive a class from
* SequencerEventHandler, overriding the method
* SequencerEventHandler::handleSequencerEvent() to
* provide your own event processing. You must provide the handler instance to
* the client using setHandler().</li>
* <li>Using QEvent listeners. To use this method, you must use one or more
* classes derived from QObject overriding the method QObject::customEvent().
* You must also use the method addListener() to add such objects to the
* client's listeners list.</li>
* <li>The third method involves signals and slots. Whenever a sequencer event
* is received, a signal eventReceived() is emitted, that can be connected to
* your own supplied slot(s) to process it.
* </ul>
* @see ALSAClient
*/
void
MidiClient::doEvents()
{
static const QMetaMethod receivedSignal = QMetaMethod::fromSignal(&MidiClient::eventReceived);
do {
int err = 0;
snd_seq_event_t* evp = nullptr;
SequencerEvent* event = nullptr;
err = snd_seq_event_input(d->m_SeqHandle, &evp);
if ((err >= 0) && (evp != nullptr)) {
switch (evp->type) {
case SND_SEQ_EVENT_NOTE:
event = new NoteEvent(evp);
break;
case SND_SEQ_EVENT_NOTEON:
event = new NoteOnEvent(evp);
break;
case SND_SEQ_EVENT_NOTEOFF:
event = new NoteOffEvent(evp);
break;
case SND_SEQ_EVENT_KEYPRESS:
event = new KeyPressEvent(evp);
break;
case SND_SEQ_EVENT_CONTROLLER:
case SND_SEQ_EVENT_CONTROL14:
case SND_SEQ_EVENT_REGPARAM:
case SND_SEQ_EVENT_NONREGPARAM:
event = new ControllerEvent(evp);
break;
case SND_SEQ_EVENT_PGMCHANGE:
event = new ProgramChangeEvent(evp);
break;
case SND_SEQ_EVENT_CHANPRESS:
event = new ChanPressEvent(evp);
break;
case SND_SEQ_EVENT_PITCHBEND:
event = new PitchBendEvent(evp);
break;
case SND_SEQ_EVENT_SYSEX:
event = new SysExEvent(evp);
break;
case SND_SEQ_EVENT_PORT_SUBSCRIBED:
case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
event = new SubscriptionEvent(evp);
break;
case SND_SEQ_EVENT_PORT_CHANGE:
case SND_SEQ_EVENT_PORT_EXIT:
case SND_SEQ_EVENT_PORT_START:
event = new PortEvent(evp);
d->m_NeedRefreshClientList = true;
break;
case SND_SEQ_EVENT_CLIENT_CHANGE:
case SND_SEQ_EVENT_CLIENT_EXIT:
case SND_SEQ_EVENT_CLIENT_START:
event = new ClientEvent(evp);
d->m_NeedRefreshClientList = true;
break;
case SND_SEQ_EVENT_SONGPOS:
case SND_SEQ_EVENT_SONGSEL:
case SND_SEQ_EVENT_QFRAME:
case SND_SEQ_EVENT_TIMESIGN:
case SND_SEQ_EVENT_KEYSIGN:
event = new ValueEvent(evp);
break;
case SND_SEQ_EVENT_SETPOS_TICK:
case SND_SEQ_EVENT_SETPOS_TIME:
case SND_SEQ_EVENT_QUEUE_SKEW:
event = new QueueControlEvent(evp);
break;
case SND_SEQ_EVENT_TEMPO:
event = new TempoEvent(evp);
break;
default:
event = new SequencerEvent(evp);
break;
}
// first, process the callback (if any)
if (d->m_handler != nullptr) {
d->m_handler->handleSequencerEvent(event->clone());
} else {
// second, process the event listeners
if (d->m_eventsEnabled) {
QObjectList::Iterator it;
for(it=d->m_listeners.begin(); it!=d->m_listeners.end(); ++it) {
QObject* sub = (*it);
QCoreApplication::postEvent(sub, event->clone());
}
} else {
// finally, process signals
if (isSignalConnected(receivedSignal)) {
Q_EMIT eventReceived(event->clone());
}
}
}
delete event;
}
}
while (snd_seq_event_input_pending(d->m_SeqHandle, 0) > 0);
}
/**
* Starts reading events from the ALSA sequencer.
*/
void
MidiClient::startSequencerInput()
{
if (d->m_Thread == nullptr) {
d->m_Thread = new SequencerInputThread(this, DEFAULT_INPUT_TIMEOUT);
}
d->m_Thread->start( d->m_Thread->m_RealTime ?
QThread::TimeCriticalPriority : QThread::InheritPriority );
}
/**
* Stops reading events from the ALSA sequencer.
*/
void
MidiClient::stopSequencerInput()
{
int counter = 0;
if (d->m_Thread != nullptr) {
if (d->m_Thread->isRunning()) {
d->m_Thread->stop();
while (!d->m_Thread->wait(500) && (counter < 10)) {
counter++;
}
if (!d->m_Thread->isFinished()) {
d->m_Thread->terminate();
}
}
delete d->m_Thread;
}
}
/**
* Reads the ALSA sequencer's clients list.
*/
void
MidiClient::readClients()
{
ClientInfo cInfo;
freeClients();
cInfo.setClient(-1);
while (snd_seq_query_next_client(d->m_SeqHandle, cInfo.m_Info) >= 0) {
cInfo.readPorts(this);
d->m_ClientList.append(cInfo);
}
d->m_NeedRefreshClientList = false;
}
/**
* Releases the list of ALSA sequencer's clients.
*/
void
MidiClient::freeClients()
{
d->m_ClientList.clear();
}
/**
* Gets the list of clients from the ALSA sequencer.
* @return the list of clients.
*/
ClientInfoList
MidiClient::getAvailableClients()
{
if (d->m_NeedRefreshClientList)
readClients();
ClientInfoList lst = d->m_ClientList; // copy
return lst;
}
/**
* Gets the ClientInfo object holding data about this client.
* @return the ClientInfo object representing this client.
*/
ClientInfo&
MidiClient::getThisClientInfo()
{
snd_seq_get_client_info(d->m_SeqHandle, d->m_Info.m_Info);
return d->m_Info;
}
/**
* Sets the data supplied by the ClientInfo object into the ALSA sequencer
* client. This allows to change the name, capabilities, type and other data
* in a single step.
*
* @param val a ClientInfo object reference
*/
void
MidiClient::setThisClientInfo(const ClientInfo& val)
{
d->m_Info = val;
snd_seq_set_client_info(d->m_SeqHandle, d->m_Info.m_Info);
}
/**
* This internal method applies the ClientInfo data to the ALSA sequencer client
*/
void
MidiClient::applyClientInfo()
{
if (d->m_SeqHandle != nullptr) {
snd_seq_set_client_info(d->m_SeqHandle, d->m_Info.m_Info);
}
}
/**
* Gets the client's public name
* @return The client's name
*/
QString
MidiClient::getClientName()
{
return d->m_Info.getName();
}
/**
* Gets the public name corresponding to the given Client ID.
* @param clientId The ID of any existing sequencer client
* @return The client's name
*/
QString
MidiClient::getClientName(const int clientId)
{
ClientInfoList::Iterator it;
if (d->m_NeedRefreshClientList)
readClients();
for (it = d->m_ClientList.begin(); it != d->m_ClientList.end(); ++it) {
if ((*it).getClientId() == clientId) {
return (*it).getName();
}
}
return QString();
}
/**
* Changes the public name of the ALSA sequencer client.
* @param newName A new public name
*/
void
MidiClient::setClientName(QString const& newName)
{
if (newName != d->m_Info.getName()) {
d->m_Info.setName(newName);
applyClientInfo();
}
}
/**
* Gets the list of MidiPort instances belonging to this client.
* @return The list of MidiPort instances.
*/
MidiPortList
MidiClient::getMidiPorts() const
{
return d->m_Ports;
}
/**
* Create and attach a new MidiPort instance to this client.
* @return The pointer to the new MidiPort instance.
*/
MidiPort*
MidiClient::createPort()
{
MidiPort* port = new MidiPort(this);
port->attach(this);
return port;
}
/**
* Attach a MidiPort instance to this client
* @param port The MidiPort to be attached
*/
void
MidiClient::portAttach(MidiPort* port)
{
if (d->m_SeqHandle != nullptr) {
DRUMSTICK_ALSA_CHECK_ERROR(snd_seq_create_port(d->m_SeqHandle, port->m_Info.m_Info));
d->m_Ports.push_back(port);
}
}
/**
* Detach a MidiPort instance from this client
* @param port The MidiPort to be detached
*/
void
MidiClient::portDetach(MidiPort* port)
{
if (d->m_SeqHandle != nullptr) {
if(port->getPortInfo()->getClient() == getClientId())
{
return;
}
DRUMSTICK_ALSA_CHECK_ERROR(snd_seq_delete_port(d->m_SeqHandle, port->getPortInfo()->getPort()));
port->setMidiClient(nullptr);
MidiPortList::iterator it;
for(it = d->m_Ports.begin(); it != d->m_Ports.end(); ++it)
{
if ((*it)->getPortInfo()->getPort() == port->getPortInfo()->getPort())
{
d->m_Ports.erase(it);
break;
}
}
}
}
/**
* Detach all the ports belonging to this client.
*/
void MidiClient::detachAllPorts()
{
if (d->m_SeqHandle != nullptr) {
QMutableListIterator<MidiPort*> it(d->m_Ports);
while (it.hasNext()) {
MidiPort* p = it.next();
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_delete_port(d->m_SeqHandle, p->getPortInfo()->getPort()));
p->setMidiClient(nullptr);
it.remove();
}
}
}
/**
* Add an event filter to the client.
* @param evtype An event filter to be added.
*/
void
MidiClient::addEventFilter(int evtype)
{
snd_seq_set_client_event_filter(d->m_SeqHandle, evtype);
}
/**
* Gets the broadcast filter usage of the client.
*
* @return The broadcast filter.
*/
bool
MidiClient::getBroadcastFilter()
{
return d->m_Info.getBroadcastFilter();
}
/**
* Sets the broadcast filter usage of the client.
*
* @param newValue The broadcast filter.
*/
void
MidiClient::setBroadcastFilter(bool newValue)
{
d->m_Info.setBroadcastFilter(newValue);
applyClientInfo();
}
/**
* Get the error-bounce usage of the client.
*
* @return The error-bounce usage.
*/
bool
MidiClient::getErrorBounce()
{
return d->m_Info.getErrorBounce();
}
/**
* Sets the error-bounce usage of the client.
*
* @param newValue The error-bounce usage.
*/
void
MidiClient::setErrorBounce(bool newValue)
{
d->m_Info.setErrorBounce(newValue);
applyClientInfo();
}
/**
* Output an event using the library output buffer.
*
* An event is once expanded on the output buffer. The output buffer will be
* drained automatically if it becomes full.
*
* @param ev The event to be sent.
* @param async Use asynchronous mode. If false, this call will block until the
* event can be delivered.
* @param timeout The maximum time to wait in synchronous mode.
*/
void
MidiClient::output(SequencerEvent* ev, bool async, int timeout)
{
pollfd* pfds = nullptr;
if (async) {
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_event_output(d->m_SeqHandle, ev->getHandle()));
} else {
int npfds = snd_seq_poll_descriptors_count(d->m_SeqHandle, POLLOUT);
pfds = (pollfd*) calloc(npfds, sizeof(pollfd));
snd_seq_poll_descriptors(d->m_SeqHandle, pfds, npfds, POLLOUT);
while (snd_seq_event_output(d->m_SeqHandle, ev->getHandle()) < 0)
{
poll(pfds, npfds, timeout);
}
free(pfds);
}
}
/**
* Output an event directly to the sequencer
*
* This function sends an event to the sequencer directly not using the library
* output buffer.
*
* @param ev The event to be sent.
* @param async Use asynchronous mode. If false, this call will block until the
* event is delivered to the sequencer.
* @param timeout The maximum time to wait in synchronous mode.
*/
void MidiClient::outputDirect(SequencerEvent* ev, bool async, int timeout)
{
if (async) {
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_event_output_direct(d->m_SeqHandle, ev->getHandle()));
} else {
int npfds = snd_seq_poll_descriptors_count(d->m_SeqHandle, POLLOUT);
pollfd* pfds = (pollfd*) calloc(npfds, sizeof(pollfd));
snd_seq_poll_descriptors(d->m_SeqHandle, pfds, npfds, POLLOUT);
while (snd_seq_event_output_direct(d->m_SeqHandle, ev->getHandle()) < 0)
{
poll(pfds, npfds, timeout);
}
free(pfds);
}
}
/**
* Output an event using the library output buffer, without draining the buffer.
*
* An event is once expanded on the output buffer. The output buffer will NOT be
* drained automatically if it becomes full.
*
* @param ev The event to be sent.
*/
void
MidiClient::outputBuffer(SequencerEvent* ev)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_event_output_buffer(d->m_SeqHandle, ev->getHandle()));
}
/**
* Drain the library output buffer.
*
* This function drains all pending events on the output buffer. The function
* returns immediately after the events are sent to the queues regardless
* whether the events are processed or not.
*
* @param async Use asynchronous mode. If false, this call will block until the
* buffer can be flushed.
* @param timeout The maximum time to wait in synchronous mode.
*/
void MidiClient::drainOutput(bool async, int timeout)
{
if (async) {
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_drain_output(d->m_SeqHandle));
} else {
int npfds = snd_seq_poll_descriptors_count(d->m_SeqHandle, POLLOUT);
pollfd* pfds = (pollfd*) calloc(npfds, sizeof(pollfd));
snd_seq_poll_descriptors(d->m_SeqHandle, pfds, npfds, POLLOUT);
while (snd_seq_drain_output(d->m_SeqHandle) < 0)
{
poll(pfds, npfds, timeout);
}
free(pfds);
}
}
/**
* Wait until all sent events are processed.
*
* This function waits until all events of this client are processed.
*/
void
MidiClient::synchronizeOutput()
{
snd_seq_sync_output_queue(d->m_SeqHandle);
}
/**
* Get the MidiQueue instance associated to this client.
* If the client is not associated to a MidiQueue, one is created.
* @return A MidiQueue instance pointer
*/
MidiQueue*
MidiClient::getQueue()
{
if (d->m_Queue == nullptr) {
createQueue();
}
return d->m_Queue;
}
/**
* Create and return a new MidiQueue associated to this client.
* @return A new MidiQueue instance.
*/
MidiQueue*
MidiClient::createQueue()
{
if (d->m_Queue != nullptr) {
delete d->m_Queue;
}
d->m_Queue = new MidiQueue(this, this);
return d->m_Queue;
}
/**
* Create and return a new MidiQueue with the given name, associated to this
* client.
* @param queueName The name for the new queue.
* @return A new MidiQueue instance.
*/
MidiQueue*
MidiClient::createQueue(QString const& queueName )
{
if (d->m_Queue != nullptr) {
delete d->m_Queue;
}
d->m_Queue = new MidiQueue(this, queueName, this);
return d->m_Queue;
}
/**
* Create a new MidiQueue instance using a queue already existing in the
* system, associating it to the client.
*
* @param queue_id An existing queue identifier.
* @return A new MidiQueue instance.
*/
MidiQueue*
MidiClient::useQueue(int queue_id)
{
if (d->m_Queue != nullptr) {
delete d->m_Queue;
}
d->m_Queue = new MidiQueue(this, queue_id, this);
return d->m_Queue;
}
/**
* Create a new MidiQueue instance using a queue already existing in the
* system, associating it to the client.
*
* @param name An existing queue name.
* @return A new MidiQueue instance.
*/
MidiQueue*
MidiClient::useQueue(const QString& name)
{
if (d->m_Queue != nullptr) {
delete d->m_Queue;
}
int queue_id = getQueueId(name);
if ( queue_id >= 0) {
d->m_Queue = new MidiQueue(this, queue_id, this);
}
return d->m_Queue;
}
/**
* Associate an existing MidiQueue instance to the client.
*
* @param queue An existing MidiQueue.
* @return The provided MidiQueue instance.
*/
MidiQueue*
MidiClient::useQueue(MidiQueue* queue)
{
if (d->m_Queue != nullptr) {
delete d->m_Queue;
}
queue->setParent(this);
d->m_Queue = queue;
return d->m_Queue;
}
/**
* Get a list of the existing queues
* @return a list of existing queues
*/
QList<int>
MidiClient::getAvailableQueues()
{
int q, err, max;
QList<int> queues;
snd_seq_queue_info_t* qinfo;
snd_seq_queue_info_alloca(&qinfo);
max = getSystemInfo().getMaxQueues();
for ( q = 0; q < max; ++q ) {
err = snd_seq_get_queue_info(d->m_SeqHandle, q, qinfo);
if (err == 0) {
queues.append(q);
}
}
return queues;
}
/**
* Gets a list of the available user ports in the system, filtered by the given
* bitmap of desired capabilities.
*
* @param filter A bitmap of capabilities.
* @return A filtered list of the available ports in the system.
*/
PortInfoList
MidiClient::filterPorts(unsigned int filter)
{
PortInfoList result;
ClientInfoList::ConstIterator itc;
PortInfoList::ConstIterator itp;
if (d->m_NeedRefreshClientList)
readClients();
for (itc = d->m_ClientList.constBegin(); itc != d->m_ClientList.constEnd(); ++itc) {
ClientInfo ci = (*itc);
if ((ci.getClientId() == SND_SEQ_CLIENT_SYSTEM) ||
(ci.getClientId() == d->m_Info.getClientId()))
continue;
PortInfoList lstPorts = ci.getPorts();
for(itp = lstPorts.constBegin(); itp != lstPorts.constEnd(); ++itp) {
PortInfo pi = (*itp);
unsigned int cap = pi.getCapability();
if ( ((filter & cap) != 0) &&
((SND_SEQ_PORT_CAP_NO_EXPORT & cap) == 0) ) {
result.append(pi);
}
}
}
return result;
}
/**
* Update the internal lists of user ports.
*/
void
MidiClient::updateAvailablePorts()
{
d->m_InputsAvail.clear();
d->m_OutputsAvail.clear();
d->m_InputsAvail = filterPorts( SND_SEQ_PORT_CAP_READ |
SND_SEQ_PORT_CAP_SUBS_READ );
d->m_OutputsAvail = filterPorts( SND_SEQ_PORT_CAP_WRITE |
SND_SEQ_PORT_CAP_SUBS_WRITE );
}
/**
* Gets the available user input ports in the system.
* @return The list of available input ports.
*/
PortInfoList
MidiClient::getAvailableInputs()
{
d->m_NeedRefreshClientList = true;
updateAvailablePorts();
return d->m_InputsAvail;
}
/**
* Gets the available user output ports in the system.
* @return The list of available output ports.
*/
PortInfoList
MidiClient::getAvailableOutputs()
{
d->m_NeedRefreshClientList = true;
updateAvailablePorts();
return d->m_OutputsAvail;
}
/**
* Adds a QObject to the listeners list. This object should override the method
* QObject::customEvent() to receive SequencerEvent instances.
* @param listener A QObject listener to be notified of received events.
* @see removeListener(), setEventsEnabled()
*/
void
MidiClient::addListener(QObject* listener)
{
d->m_listeners.append(listener);
}
/**
* Removes a QObject listener from the listeners list.
* @param listener listener A QObject listener to be removed of received events.
* @see addListener(), setEventsEnabled()
*/
void
MidiClient::removeListener(QObject* listener)
{
d->m_listeners.removeAll(listener);
}
/**
* Enables the notification of received SequencerEvent instances to the listeners
* registered with addListener()
* @param bEnabled The new state of the events delivering mode.
* @see addListener(), removeListener(), setEventsEnabled()
*/
void
MidiClient::setEventsEnabled(bool bEnabled)
{
if (bEnabled != d->m_eventsEnabled) {
d->m_eventsEnabled = bEnabled;
}
}
/**
* Gets a SystemInfo instance with the updated state of the system.
* @return The updated system info.
*/
SystemInfo&
MidiClient::getSystemInfo()
{
snd_seq_system_info(d->m_SeqHandle, d->m_sysInfo.m_Info);
return d->m_sysInfo;
}
/**
* Gets a PoolInfo instance with an updated state of the client memory pool
* @return The updated memory pool state.
*/
PoolInfo&
MidiClient::getPoolInfo()
{
snd_seq_get_client_pool(d->m_SeqHandle, d->m_poolInfo.m_Info);
return d->m_poolInfo;
}
/**
* Applies (updates) the client's PoolInfo data into the system.
* @param info The PoolInfo reference to be applied to the client.
*/
void
MidiClient::setPoolInfo(const PoolInfo& info)
{
d->m_poolInfo = info;
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_client_pool(d->m_SeqHandle, d->m_poolInfo.m_Info));
}
/**
* Resets the client input pool.
* @see dropInput()
*/
void
MidiClient::resetPoolInput()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_reset_pool_input(d->m_SeqHandle));
}
/**
* Resets the client output pool.
* @see dropOutput()
*/
void
MidiClient::resetPoolOutput()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_reset_pool_output(d->m_SeqHandle));
}
/**
* Sets the size of the client's input pool.
* @param size The new size
*/
void
MidiClient::setPoolInput(int size)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_client_pool_input(d->m_SeqHandle, size));
}
/**
* Sets the size of the client's output pool.
* @param size The new size
*/
void
MidiClient::setPoolOutput(int size)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_client_pool_output(d->m_SeqHandle, size));
}
/**
* Sets the room size of the client's output pool.
* @param size The new size
*/
void
MidiClient::setPoolOutputRoom(int size)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_client_pool_output_room(d->m_SeqHandle, size));
}
/**
* Clears the client's input buffer and and remove events in sequencer queue.
* @see resetPoolInput()
*/
void
MidiClient::dropInput()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_drop_input(d->m_SeqHandle));
}
/**
* Remove all events on user-space input buffer.
* @see dropInput()
*/
void
MidiClient::dropInputBuffer()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_drop_input_buffer(d->m_SeqHandle));
}
/**
* Clears the client's output buffer and and remove events in sequencer queue.
*
* This method removes all events on both user-space output buffer and output
* memory pool on kernel.
* @see resetPoolOutput()
*/
void
MidiClient::dropOutput()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_drop_output(d->m_SeqHandle));
}
/**
* Removes all events on the library output buffer.
*
* Removes all events on the user-space output buffer. Unlike dropOutput(), this
* method doesn't remove events on the client's output memory pool.
* @see dropOutput()
*/
void
MidiClient::dropOutputBuffer()
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_drop_output_buffer(d->m_SeqHandle));
}
/**
* Removes events on input/output buffers and pools.
* Removes matching events with the given condition from input/output buffers
* and pools. The removal condition is specified in the spec argument.
* @param spec A RemoveEvents instance specifying the removal condition.
*/
void
MidiClient::removeEvents(const RemoveEvents* spec)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_remove_events(d->m_SeqHandle, spec->m_Info));
}
/**
* Extracts (and removes) the first event in the output buffer.
* @return The extracted event.
*/
SequencerEvent*
MidiClient::extractOutput()
{
snd_seq_event_t* ev;
if (DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_extract_output(d->m_SeqHandle, &ev) == 0)) {
return new SequencerEvent(ev);
}
return nullptr;
}
/**
* Returns the size of pending events on the output buffer.
*
* @return The size of pending events.
*/
int
MidiClient::outputPending()
{
return snd_seq_event_output_pending(d->m_SeqHandle);
}
/**
* Gets the size of the events on the input buffer.
*
* If there are events remaining on the user-space input buffer, this method
* returns the total size of events on it. If the argument is true, this method
* checks the presence of events on the sequencer FIFO, and when events exist
* they are transferred to the input buffer, and the number of received events
* are returned. If the argument is false and no events remain on the input
* buffer, this method simply returns zero.
*
* @param fetch Check and fetch the sequencer input pool.
* @return The size in bytes of the remaining input events on the buffer.
*/
int
MidiClient::inputPending(bool fetch)
{
return snd_seq_event_input_pending(d->m_SeqHandle, fetch ? 1 : 0);
}
/**
* Gets the queue's numeric identifier corresponding to the provided name.
*
* @param name The name string to query.
* @return The number of the matching queue.
*/
int
MidiClient::getQueueId(const QString& name)
{
return snd_seq_query_named_queue(d->m_SeqHandle, name.toLocal8Bit().data());
}
/**
* Returns the number of poll descriptors.
* @param events Poll events to be checked (POLLIN and POLLOUT).
* @return The number of poll descriptors.
*/
int
MidiClient::getPollDescriptorsCount(short events)
{
return snd_seq_poll_descriptors_count(d->m_SeqHandle, events);
}
/**
* Get poll descriptors.
*
* Get poll descriptors assigned to the sequencer handle. Since a sequencer
* handle can duplex streams, you need to set which direction(s) is/are polled
* in events argument. When POLLIN bit is specified, the incoming events to the
* ports are checked.
*
* @param pfds Array of poll descriptors
* @param space Space in the poll descriptor array
* @param events Polling events to be checked (POLLIN and POLLOUT)
* @return Count of filled descriptors
*/
int
MidiClient::pollDescriptors( struct pollfd *pfds, unsigned int space,
short events )
{
return snd_seq_poll_descriptors(d->m_SeqHandle, pfds, space, events);
}
/**
* Gets the number of returned events from poll descriptors
* @param pfds Array of poll descriptors.
* @param nfds Count of poll descriptors.
* @return Number of returned events.
*/
unsigned short
MidiClient::pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds)
{
unsigned short revents;
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_poll_descriptors_revents( d->m_SeqHandle,
pfds, nfds,
&revents ));
return revents;
}
/**
* Gets the internal sequencer device name
* @return The device name.
*/
const char *
MidiClient::_getDeviceName()
{
return snd_seq_name(d->m_SeqHandle);
}
/**
* Sets the client name
* @param name The new client name.
*/
void
MidiClient::_setClientName(const char *name)
{
DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_client_name(d->m_SeqHandle, name));
}
/**
* Create an ALSA sequencer port, without using MidiPort.
* @param name The name of the new port.
* @param caps The new port capabilities.
* @param type The type of the new port.
* @return The port numeric identifier.
*/
int
MidiClient::createSimplePort( const char *name,
unsigned int caps,
unsigned int type )
{
return DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_create_simple_port( d->m_SeqHandle,
name, caps, type ));
}
/**
* Remove an ALSA sequencer port.
* @param port The numeric identifier of the port.
*/
void
MidiClient::deleteSimplePort(int port)
{
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_delete_simple_port( d->m_SeqHandle, port ));
}
/**
* Subscribe one port from another arbitrary sequencer client:port.
* @param myport The number of the internal port.
* @param client The external client's identifier.
* @param port The external port's identifier.
*/
void
MidiClient::connectFrom(int myport, int client, int port)
{
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_connect_from(d->m_SeqHandle, myport, client, port ));
}
/**
* Subscribe one port to another arbitrary sequencer client:port.
* @param myport The number of the internal port.
* @param client The external client's identifier.
* @param port The external port's identifier.
*/
void
MidiClient::connectTo(int myport, int client, int port)
{
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_connect_to(d->m_SeqHandle, myport, client, port ));
}
/**
* Unsubscribe one port from another arbitrary sequencer client:port.
* @param myport The number of the internal port.
* @param client The external client's identifier.
* @param port The external port's identifier.
*/
void
MidiClient::disconnectFrom(int myport, int client, int port)
{
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_disconnect_from(d->m_SeqHandle, myport, client, port ));
}
/**
* Unsubscribe one port to another arbitrary sequencer client:port.
* @param myport The number of the internal port.
* @param client The external client's identifier.
* @param port The external port's identifier.
*/
void
MidiClient::disconnectTo(int myport, int client, int port)
{
DRUMSTICK_ALSA_CHECK_WARNING( snd_seq_disconnect_to(d->m_SeqHandle, myport, client, port ));
}
/**
* Parse a text address representation, returning an ALSA address record.
*
* This function can be used as a replacement of the standard ALSA function
* snd_seq_parse_address().
*
* @param straddr source text address representation
* @param addr returned ALSA address record
* @return true if the text address was successfully parsed
* @since 0.3.1
*/
bool
MidiClient::parseAddress( const QString& straddr, snd_seq_addr& addr )
{
bool ok(false);
QString testClient, testPort;
ClientInfoList::ConstIterator cit;
int pos = straddr.indexOf(':');
if (pos > -1) {
testClient = straddr.left(pos);
testPort = straddr.mid(pos+1);
} else {
testClient = straddr;
testPort = '0';
}
addr.client = testClient.toInt(&ok);
if (ok)
addr.port = testPort.toInt(&ok);
if (!ok) {
if (d->m_NeedRefreshClientList)
readClients();
for ( cit = d->m_ClientList.constBegin();
cit != d->m_ClientList.constEnd(); ++cit ) {
ClientInfo ci = *cit;
if (testClient.compare(ci.getName(), Qt::CaseInsensitive) == 0) {
addr.client = ci.getClientId();
addr.port = testPort.toInt(&ok);
return ok;
}
}
}
return ok;
}
/**
* Returns true or false depending on the input thread state.
* @return true if the input thread is stopped.
*/
bool
MidiClient::SequencerInputThread::stopped()
{
QReadLocker locker(&m_mutex);
return m_Stopped;
}
/**
* Stops the input thread.
*/
void
MidiClient::SequencerInputThread::stop()
{
QWriteLocker locker(&m_mutex);
m_Stopped = true;
}
#if defined(RTKIT_SUPPORT)
static pid_t _gettid() {
return (pid_t) ::syscall(SYS_gettid);
}
#endif
void
MidiClient::SequencerInputThread::setRealtimePriority()
{
struct sched_param p;
int rt, policy = SCHED_RR | SCHED_RESET_ON_FORK;
quint32 priority = 6;
#if defined(RTKIT_SUPPORT)
bool ok;
quint32 max_prio;
quint64 thread;
struct rlimit old_limit, new_limit;
long long max_rttime;
#endif
::memset(&p, 0, sizeof(p));
p.sched_priority = priority;
rt = ::pthread_setschedparam(::pthread_self(), policy, &p);
if (rt != 0) {
#if defined(RTKIT_SUPPORT)
const QString rtkit_service =
QStringLiteral("org.freedesktop.RealtimeKit1");
const QString rtkit_path =
QStringLiteral("/org/freedesktop/RealtimeKit1");
const QString rtkit_iface = rtkit_service;
thread = _gettid();
QDBusConnection bus = QDBusConnection::systemBus();
QDBusInterface realtimeKit(rtkit_service, rtkit_path, rtkit_iface, bus);
QVariant maxRTPrio = realtimeKit.property("MaxRealtimePriority");
max_prio = maxRTPrio.toUInt(&ok);
if (!ok) {
qWarning() << "invalid property RealtimeKit.MaxRealtimePriority";
return;
}
if (priority > max_prio)
priority = max_prio;
QVariant maxRTNSec = realtimeKit.property("RTTimeNSecMax");
max_rttime = maxRTNSec.toLongLong(&ok);
if (!ok || max_rttime < 0) {
qWarning() << "invalid property RealtimeKit.RTTimeNSecMax";
return;
}
new_limit.rlim_cur = new_limit.rlim_max = max_rttime;
rt = ::getrlimit(RLIMIT_RTTIME, &old_limit);
if (rt < 0) {
qWarning() << "getrlimit() failed. err=" << rt << ::strerror(rt);
return;
}
rt = ::setrlimit(RLIMIT_RTTIME, &new_limit);
if ( rt < 0) {
qWarning() << "setrlimit() failed, err=" << rt << ::strerror(rt);
return;
}
QDBusMessage reply = realtimeKit.call("MakeThreadRealtime", thread, priority);
if (reply.type() == QDBusMessage::ErrorMessage )
qWarning() << "error returned by RealtimeKit.MakeThreadRealtime:"
<< reply.errorMessage();
#endif
} else {
qWarning() << "pthread_setschedparam() failed, err="
<< rt << ::strerror(rt);
}
}
/**
* Main input thread process loop.
*/
void
MidiClient::SequencerInputThread::run()
{
if ( priority() == TimeCriticalPriority ) {
setRealtimePriority();
}
if (m_MidiClient != nullptr) {
int npfd = snd_seq_poll_descriptors_count(m_MidiClient->getHandle(), POLLIN);
pollfd* pfd = (pollfd *) calloc(npfd, sizeof(pollfd));
try
{
snd_seq_poll_descriptors(m_MidiClient->getHandle(), pfd, npfd, POLLIN);
while (!stopped() && (m_MidiClient != nullptr))
{
int rt = poll(pfd, npfd, m_Wait);
if (rt > 0) {
m_MidiClient->doEvents();
}
}
}
catch (...)
{
qWarning() << "exception in input thread";
}
free(pfd);
}
}
/**
* Default constructor
*/
ClientInfo::ClientInfo()
{
snd_seq_client_info_malloc(&m_Info);
}
/**
* Copy constructor
* @param other Another ClientInfo reference to be copied
*/
ClientInfo::ClientInfo(const ClientInfo& other)
{
snd_seq_client_info_malloc(&m_Info);
snd_seq_client_info_copy(m_Info, other.m_Info);
m_Ports = other.m_Ports;
}
/**
* Copy constructor
* @param other An existing ALSA client info object
*/
ClientInfo::ClientInfo(snd_seq_client_info_t* other)
{
snd_seq_client_info_malloc(&m_Info);
snd_seq_client_info_copy(m_Info, other);
}
/**
* Constructor
* @param seq A MidiClient object
* @param id A numeric client id
*/
ClientInfo::ClientInfo(MidiClient* seq, int id)
{
snd_seq_client_info_malloc(&m_Info);
snd_seq_get_any_client_info(seq->getHandle(), id, m_Info);
}
/**
* Destructor
*/
ClientInfo::~ClientInfo()
{
freePorts();
snd_seq_client_info_free(m_Info);
}
/**
* Clone the client info object.
* @return A pointer to the new object.
*/
ClientInfo*
ClientInfo::clone()
{
return new ClientInfo(m_Info);
}
/**
* Assignment operator
* @param other Another ClientInfo object
* @return This object
*/
ClientInfo&
ClientInfo::operator=(const ClientInfo& other)
{
if (this == &other)
return *this;
snd_seq_client_info_copy(m_Info, other.m_Info);
m_Ports = other.m_Ports;
return *this;
}
/**
* Gets the client's numeric identifier.
* @return The client's numeric identifier.
*/
int
ClientInfo::getClientId()
{
return snd_seq_client_info_get_client(m_Info);
}
/**
* Gets the client's type
* @return The client's type.
*/
snd_seq_client_type_t
ClientInfo::getClientType()
{
return snd_seq_client_info_get_type(m_Info);
}
/**
* Gets the client's name
* @return The client's name.
*/
QString
ClientInfo::getName()
{
return QString(snd_seq_client_info_get_name(m_Info));
}
/**
* Gets the client's broadcast filter
* @return The client's broadcast filter.
*/
bool
ClientInfo::getBroadcastFilter()
{
return (snd_seq_client_info_get_broadcast_filter(m_Info) != 0);
}
/**
* Gets the client's error bounce
* @return The client's error bounce.
*/
bool
ClientInfo::getErrorBounce()
{
return (snd_seq_client_info_get_error_bounce(m_Info) != 0);
}
/**
* Gets the client's event filter.
* @return The client's event filter.
* @deprecated Use isFiltered() instead.
*/
const unsigned char*
ClientInfo::getEventFilter()
{
return snd_seq_client_info_get_event_filter(m_Info);
}
/**
* Gets the client's port count.
* @return The client's port count.
*/
int
ClientInfo::getNumPorts()
{
return snd_seq_client_info_get_num_ports(m_Info);
}
/**
* Gets the number of lost events.
* @return The number of lost events.
*/
int
ClientInfo::getEventLost()
{
return snd_seq_client_info_get_event_lost(m_Info);
}
/**
* Sets the client identifier number.
* @param client The client identifier number.
*/
void
ClientInfo::setClient(int client)
{
snd_seq_client_info_set_client(m_Info, client);
}
/**
* Sets the client name.
* @param name The client name.
*/
void
ClientInfo::setName(QString name)
{
snd_seq_client_info_set_name(m_Info, name.toLocal8Bit().data());
}
/**
* Sets the broadcast filter.
* @param val The broadcast filter.
*/
void
ClientInfo::setBroadcastFilter(bool val)
{
snd_seq_client_info_set_broadcast_filter(m_Info, val ? 1 : 0);
}
/**
* Sets the error bounce.
* @param val The error bounce.
*/
void
ClientInfo::setErrorBounce(bool val)
{
snd_seq_client_info_set_error_bounce(m_Info, val ? 1 : 0);
}
/**
* Sets the event filter.
* @param filter The event filter.
* @deprecated Use addFilter() instead.
*/
void
ClientInfo::setEventFilter(unsigned char *filter)
{
snd_seq_client_info_set_event_filter(m_Info, filter);
}
/**
* Read the client ports.
* @param seq The client instance.
*/
void
ClientInfo::readPorts(MidiClient* seq)
{
PortInfo info;
freePorts();
info.setClient(getClientId());
info.setClientName(getName());
info.setPort(-1);
while (snd_seq_query_next_port(seq->getHandle(), info.m_Info) >= 0) {
info.readSubscribers(seq);
m_Ports.append(info);
}
}
/**
* Release the ports list.
*/
void
ClientInfo::freePorts()
{
m_Ports.clear();
}
/**
* Gets the ports list.
* @return The ports list.
*/
PortInfoList
ClientInfo::getPorts() const
{
PortInfoList lst = m_Ports; // copy
return lst;
}
/**
* Gets the size of the internal object.
* @return The size of the internal object.
*/
int
ClientInfo::getSizeOfInfo() const
{
return snd_seq_client_info_sizeof();
}
#if SND_LIB_VERSION > 0x010010
/**
* Adds an event type to the client's filter.
*
* @param eventType The new event's type.
*/
void
ClientInfo::addFilter(int eventType)
{
snd_seq_client_info_event_filter_add(m_Info, eventType);
}
/**
* Checks id the given event's type is filtered.
* @param eventType The event's type.
* @return true if the event type is filtered
*/
bool
ClientInfo::isFiltered(int eventType)
{
return (snd_seq_client_info_event_filter_check(m_Info, eventType) != 0);
}
/**
* Clear the client's event filter
*/
void
ClientInfo::clearFilter()
{
snd_seq_client_info_event_filter_clear(m_Info);
}
/**
* Removes the event type from the client's filter.
* @param eventType The event's type.
*/
void
ClientInfo::removeFilter(int eventType)
{
snd_seq_client_info_event_filter_del(m_Info, eventType);
}
#endif
/**
* Default constructor
*/
SystemInfo::SystemInfo()
{
snd_seq_system_info_malloc(&m_Info);
}
/**
* Copy constructor
* @param other Another SystemInfo object reference to be copied
*/
SystemInfo::SystemInfo(const SystemInfo& other)
{
snd_seq_system_info_malloc(&m_Info);
snd_seq_system_info_copy(m_Info, other.m_Info);
}
/**
* Copy constructor
* @param other Another ALSA system info object to be copied
*/
SystemInfo::SystemInfo(snd_seq_system_info_t* other)
{
snd_seq_system_info_malloc(&m_Info);
snd_seq_system_info_copy(m_Info, other);
}
/**
* Constructor
* @param seq A MidiClient object
*/
SystemInfo::SystemInfo(MidiClient* seq)
{
snd_seq_system_info_malloc(&m_Info);
snd_seq_system_info(seq->getHandle(), m_Info);
}
/**
* Destructor
*/
SystemInfo::~SystemInfo()
{
snd_seq_system_info_free(m_Info);
}
/**
* Clone the system info object
* @return A pointer to the new object
*/
SystemInfo*
SystemInfo::clone()
{
return new SystemInfo(m_Info);
}
/**
* Assignment operator
* @param other Another SystemInfo object
* @return This object
*/
SystemInfo&
SystemInfo::operator=(const SystemInfo& other)
{
if (this == &other)
return *this;
snd_seq_system_info_copy(m_Info, other.m_Info);
return *this;
}
/**
* Get the system's maximum number of clients.
* @return The maximum number of clients.
*/
int SystemInfo::getMaxClients()
{
return snd_seq_system_info_get_clients(m_Info);
}
/**
* Get the system's maximum number of ports.
* @return The maximum number of ports.
*/
int SystemInfo::getMaxPorts()
{
return snd_seq_system_info_get_ports(m_Info);
}
/**
* Get the system's maximum number of queues.
* @return The system's maximum number of queues.
*/
int SystemInfo::getMaxQueues()
{
return snd_seq_system_info_get_queues(m_Info);
}
/**
* Get the system's maximum number of channels.
* @return The system's maximum number of channels.
*/
int SystemInfo::getMaxChannels()
{
return snd_seq_system_info_get_channels(m_Info);
}
/**
* Get the system's current number of queues.
* @return The system's current number of queues.
*/
int SystemInfo::getCurrentQueues()
{
return snd_seq_system_info_get_cur_queues(m_Info);
}
/**
* Get the system's current number of clients.
* @return The system's current number of clients.
*/
int SystemInfo::getCurrentClients()
{
return snd_seq_system_info_get_cur_clients(m_Info);
}
/**
* Get the system's info object size.
* @return The system's info object size.
*/
int SystemInfo::getSizeOfInfo() const
{
return snd_seq_system_info_sizeof();
}
/**
* Default constructor
*/
PoolInfo::PoolInfo()
{
snd_seq_client_pool_malloc(&m_Info);
}
/**
* Copy constructor
* @param other Another PoolInfo object reference to be copied
*/
PoolInfo::PoolInfo(const PoolInfo& other)
{
snd_seq_client_pool_malloc(&m_Info);
snd_seq_client_pool_copy(m_Info, other.m_Info);
}
/**
* Copy constructor
* @param other An ALSA pool info object to be copied
*/
PoolInfo::PoolInfo(snd_seq_client_pool_t* other)
{
snd_seq_client_pool_malloc(&m_Info);
snd_seq_client_pool_copy(m_Info, other);
}
/**
* Constructor
* @param seq A MidiClient object
*/
PoolInfo::PoolInfo(MidiClient* seq)
{
snd_seq_client_pool_malloc(&m_Info);
snd_seq_get_client_pool(seq->getHandle(), m_Info);
}
/**
* Destructor
*/
PoolInfo::~PoolInfo()
{
snd_seq_client_pool_free(m_Info);
}
/**
* Clone the pool info obeject
* @return A pointer to the new object
*/
PoolInfo*
PoolInfo::clone()
{
return new PoolInfo(m_Info);
}
/**
* Assignment operator
* @param other Another PoolInfo object reference to be copied
* @return This object
*/
PoolInfo&
PoolInfo::operator=(const PoolInfo& other)
{
if (this == &other)
return *this;
snd_seq_client_pool_copy(m_Info, other.m_Info);
return *this;
}
/**
* Gets the client ID for this object.
* @return The client ID.
*/
int
PoolInfo::getClientId()
{
return snd_seq_client_pool_get_client(m_Info);
}
/**
* Gets the available size on input pool.
* @return The available size on input pool.
*/
int
PoolInfo::getInputFree()
{
return snd_seq_client_pool_get_input_free(m_Info);
}
/**
* Gets the input pool size.
* @return The input pool size.
*/
int
PoolInfo::getInputPool()
{
return snd_seq_client_pool_get_input_pool(m_Info);
}
/**
* Gets the available size on output pool.
* @return The available size on output pool.
*/
int
PoolInfo::getOutputFree()
{
return snd_seq_client_pool_get_output_free(m_Info);
}
/**
* Gets the output pool size.
* @return The output pool size.
*/
int
PoolInfo::getOutputPool()
{
return snd_seq_client_pool_get_output_pool(m_Info);
}
/**
* Gets the output room size.
* The output room is the minimum pool size for select/blocking mode.
* @return The output room size.
*/
int
PoolInfo::getOutputRoom()
{
return snd_seq_client_pool_get_output_room(m_Info);
}
/**
* Set the input pool size.
* @param size The input pool size.
*/
void
PoolInfo::setInputPool(int size)
{
snd_seq_client_pool_set_input_pool(m_Info, size);
}
/**
* Sets the output pool size.
* @param size The output pool size.
*/
void
PoolInfo::setOutputPool(int size)
{
snd_seq_client_pool_set_output_pool(m_Info, size);
}
/**
* Sets the output room size.
* The output room is the minimum pool size for select/blocking mode.
*
* @param size Output room size
*/
void
PoolInfo::setOutputRoom(int size)
{
snd_seq_client_pool_set_output_room(m_Info, size);
}
/**
* Gets the size of the client pool object.
* @return The size of the client pool object.
*/
int
PoolInfo::getSizeOfInfo() const
{
return snd_seq_client_pool_sizeof();
}
#if SND_LIB_VERSION > 0x010004
/**
* Gets the runtime ALSA library version string
* @return string representing the runtime ALSA library version
* @since 0.3.0
*/
QString
getRuntimeALSALibraryVersion()
{
return QString(snd_asoundlib_version());
}
/**
* Gets the runtime ALSA library version number
* @return integer representing the runtime ALSA library version
* @since 0.3.0
*/
int
getRuntimeALSALibraryNumber()
{
QRegularExpression rx("(\\d+)");
QString str = getRuntimeALSALibraryVersion();
bool ok;
int result = 0, j = 0;
QRegularExpressionMatchIterator i = rx.globalMatch(str);
while (i.hasNext() && (j < 3)) {
QRegularExpressionMatch m = i.next();
int v = m.captured(1).toInt(&ok);
if (ok) {
result <<= 8;
result += v;
}
j++;
}
return result;
}
#endif // SND_LIB_VERSION > 0x010004
/**
* Gets the runtime ALSA drivers version string
* @return string representing the runtime ALSA drivers version
* @since 0.3.0
*/
QString
getRuntimeALSADriverVersion()
{
QRegularExpression rx("([\\d\\.]+)");
QString s;
QFile f("/proc/asound/version");
if (f.open(QFile::ReadOnly)) {
QTextStream str(&f);
QString sub = str.readLine().trimmed();
QRegularExpressionMatch m = rx.match(sub);
if (m.hasMatch()) {
s = m.captured(1);
}
}
return s;
}
/**
* Gets the runtime ALSA drivers version number
* @return integer representing the runtime ALSA drivers version
* @since 0.3.0
*/
int
getRuntimeALSADriverNumber()
{
QRegularExpression rx("(\\d+)");
QString str = getRuntimeALSADriverVersion();
bool ok;
int result = 0, j = 0;
QRegularExpressionMatchIterator i = rx.globalMatch(str);
while (i.hasNext() && (j < 3)) {
QRegularExpressionMatch m = i.next();
int v = m.captured(1).toInt(&ok);
if (ok) {
result <<= 8;
result += v;
}
j++;
}
return result;
}
/**
* ALSA library version at build time.
*
* This string corresponds to the compilation library, which may be
* different to the runtime library.
* @return ALSA runtime library formatted as a QString
* @see getRuntimeALSALibraryVersion
*/
QString getCompiledALSALibraryVersion()
{
return QStringLiteral(SND_LIB_VERSION_STR);
}
/**
* @brief getDrumstickLibraryVersion provides the Drumstick version as an edited QString
* @return Drumstick library version
*/
QString getDrumstickLibraryVersion()
{
return QStringLiteral(QT_STRINGIFY(VERSION));
}
/** @} */
} // namespace ALSA
} // namespace drumstick
|