jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1951 lines
45KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #if defined(__APPLE__) && defined(__POWERPC__)
  18. #include "pThreadUtilities.h"
  19. #include "ipc.h"
  20. #include "fakepoll.h"
  21. #else
  22. #include <sys/poll.h>
  23. #endif
  24. #include <sys/socket.h>
  25. #include <sys/un.h>
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/ipc.h>
  31. #include <sys/mman.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <regex.h>
  36. #include <signal.h>
  37. #include <config.h>
  38. #include <jack/jack.h>
  39. #include <jack/internal.h>
  40. #include <jack/engine.h>
  41. #include <jack/pool.h>
  42. #include <jack/time.h>
  43. #include <jack/jslist.h>
  44. #include <jack/version.h>
  45. #include <jack/shm.h>
  46. #include "local.h"
  47. #ifdef WITH_TIMESTAMPS
  48. #include <jack/timestamps.h>
  49. #endif /* WITH_TIMESTAMPS */
  50. #ifdef DEFAULT_TMP_DIR
  51. char *jack_server_dir = DEFAULT_TMP_DIR;
  52. #else
  53. char *jack_server_dir = "/tmp";
  54. #endif
  55. void
  56. jack_set_server_dir (const char *path)
  57. {
  58. jack_error ("jack_set_server_dir() is deprecated.\n"
  59. "Please contact the program's author");
  60. jack_server_dir = strdup (path);
  61. }
  62. static pthread_mutex_t client_lock;
  63. static pthread_cond_t client_ready;
  64. void *jack_zero_filled_buffer = NULL;
  65. #define event_fd pollfd[0].fd
  66. #define graph_wait_fd pollfd[1].fd
  67. typedef struct {
  68. int status;
  69. struct _jack_client *client;
  70. const char *client_name;
  71. } client_info;
  72. void
  73. jack_error (const char *fmt, ...)
  74. {
  75. va_list ap;
  76. char buffer[300];
  77. va_start (ap, fmt);
  78. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  79. jack_error_callback (buffer);
  80. va_end (ap);
  81. }
  82. void
  83. default_jack_error_callback (const char *desc)
  84. {
  85. fprintf(stderr, "%s\n", desc);
  86. }
  87. void
  88. silent_jack_error_callback (const char *desc)
  89. {
  90. }
  91. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  92. static int
  93. oop_client_deliver_request (void *ptr, jack_request_t *req)
  94. {
  95. int wok, rok;
  96. jack_client_t *client = (jack_client_t*) ptr;
  97. wok = (write (client->request_fd, req, sizeof (*req))
  98. == sizeof (*req));
  99. rok = (read (client->request_fd, req, sizeof (*req))
  100. == sizeof (*req));
  101. if (wok && rok) /* everything OK? */
  102. return req->status;
  103. req->status = -1; /* request failed */
  104. /* check for server shutdown */
  105. if (client->engine->engine_ok == 0)
  106. return req->status;
  107. /* otherwise report errors */
  108. if (!wok)
  109. jack_error ("cannot send request type %d to server",
  110. req->type);
  111. if (!rok)
  112. jack_error ("cannot read result for request type %d from"
  113. " server (%s)", req->type, strerror (errno));
  114. return req->status;
  115. }
  116. int
  117. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  118. {
  119. /* indirect through the function pointer that was set
  120. either by jack_client_new() (external) or handle_new_client()
  121. in the server.
  122. */
  123. return client->control->deliver_request (client->control->deliver_arg, req);
  124. }
  125. jack_client_t *
  126. jack_client_alloc ()
  127. {
  128. jack_client_t *client;
  129. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  130. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  131. client->pollmax = 2;
  132. client->request_fd = -1;
  133. client->event_fd = -1;
  134. client->graph_wait_fd = -1;
  135. client->graph_next_fd = -1;
  136. client->ports = NULL;
  137. client->engine = NULL;
  138. client->control = NULL;
  139. client->thread_ok = FALSE;
  140. client->first_active = TRUE;
  141. client->on_shutdown = NULL;
  142. client->n_port_types = 0;
  143. client->port_segment = NULL;
  144. return client;
  145. }
  146. jack_client_t *
  147. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  148. {
  149. jack_client_t* client;
  150. client = jack_client_alloc ();
  151. client->control = cc;
  152. client->engine = engine->control;
  153. client->n_port_types = client->engine->n_port_types;
  154. client->port_segment = &engine->port_segment[0];
  155. return client;
  156. }
  157. static void
  158. jack_client_free (jack_client_t *client)
  159. {
  160. if (client->pollfd) {
  161. free (client->pollfd);
  162. }
  163. free (client);
  164. }
  165. void
  166. jack_client_invalidate_port_buffers (jack_client_t *client)
  167. {
  168. JSList *node;
  169. jack_port_t *port;
  170. /* This releases all local memory owned by input ports
  171. and sets the buffer pointer to NULL. This will cause
  172. jack_port_get_buffer() to reallocate space for the
  173. buffer on the next call (if there is one).
  174. */
  175. for (node = client->ports; node; node = jack_slist_next (node)) {
  176. port = (jack_port_t *) node->data;
  177. if (port->shared->flags & JackPortIsInput) {
  178. if (port->mix_buffer) {
  179. jack_pool_release (port->mix_buffer);
  180. port->mix_buffer = NULL;
  181. }
  182. }
  183. }
  184. }
  185. int
  186. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  187. {
  188. jack_port_t *control_port;
  189. jack_port_t *other;
  190. JSList *node;
  191. switch (event->type) {
  192. case PortConnected:
  193. other = jack_port_new (client, event->y.other_id, client->engine);
  194. control_port = jack_port_by_id (client, event->x.self_id);
  195. pthread_mutex_lock (&control_port->connection_lock);
  196. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  197. pthread_mutex_unlock (&control_port->connection_lock);
  198. break;
  199. case PortDisconnected:
  200. control_port = jack_port_by_id (client, event->x.self_id);
  201. pthread_mutex_lock (&control_port->connection_lock);
  202. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  203. other = (jack_port_t *) node->data;
  204. if (other->shared->id == event->y.other_id) {
  205. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  206. jack_slist_free_1 (node);
  207. free (other);
  208. break;
  209. }
  210. }
  211. pthread_mutex_unlock (&control_port->connection_lock);
  212. break;
  213. default:
  214. /* impossible */
  215. break;
  216. }
  217. return 0;
  218. }
  219. static int
  220. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  221. {
  222. char path[PATH_MAX+1];
  223. if (client->graph_wait_fd >= 0) {
  224. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  225. close (client->graph_wait_fd);
  226. client->graph_wait_fd = -1;
  227. }
  228. if (client->graph_next_fd >= 0) {
  229. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  230. close (client->graph_next_fd);
  231. client->graph_next_fd = -1;
  232. }
  233. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  234. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  235. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  236. return -1;
  237. }
  238. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  239. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  240. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  241. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  242. return -1;
  243. }
  244. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  245. /* If the client registered its own callback for graph order events,
  246. execute it now.
  247. */
  248. if (client->control->graph_order) {
  249. client->control->graph_order (client->control->graph_order_arg);
  250. }
  251. return 0;
  252. }
  253. static int
  254. server_connect (int which)
  255. {
  256. int fd;
  257. struct sockaddr_un addr;
  258. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  259. jack_error ("cannot create client socket (%s)",
  260. strerror (errno));
  261. return -1;
  262. }
  263. addr.sun_family = AF_UNIX;
  264. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  265. jack_server_dir, which);
  266. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  267. close (fd);
  268. return -1;
  269. }
  270. return fd;
  271. }
  272. static int
  273. server_event_connect (jack_client_t *client)
  274. {
  275. int fd;
  276. struct sockaddr_un addr;
  277. jack_client_connect_ack_request_t req;
  278. jack_client_connect_ack_result_t res;
  279. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  280. jack_error ("cannot create client event socket (%s)",
  281. strerror (errno));
  282. return -1;
  283. }
  284. addr.sun_family = AF_UNIX;
  285. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  286. jack_server_dir);
  287. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  288. jack_error ("cannot connect to jack server for events",
  289. strerror (errno));
  290. close (fd);
  291. return -1;
  292. }
  293. req.client_id = client->control->id;
  294. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  295. jack_error ("cannot write event connect request to server (%s)",
  296. strerror (errno));
  297. close (fd);
  298. return -1;
  299. }
  300. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  301. jack_error ("cannot read event connect result from server (%s)",
  302. strerror (errno));
  303. close (fd);
  304. return -1;
  305. }
  306. if (res.status != 0) {
  307. jack_error ("cannot connect to server for event stream (%s)",
  308. strerror (errno));
  309. close (fd);
  310. return -1;
  311. }
  312. return fd;
  313. }
  314. static void
  315. _start_server (void)
  316. {
  317. FILE* fp = 0;
  318. char filename[255];
  319. char arguments[255];
  320. char buffer[255];
  321. char* command = 0;
  322. size_t pos = 0;
  323. size_t result = 0;
  324. char** argv = 0;
  325. int i = 0;
  326. int good = 0;
  327. int ret;
  328. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  329. fp = fopen(filename, "r");
  330. if (!fp) {
  331. fp = fopen("/etc/jackd.conf", "r");
  332. }
  333. if (fp) {
  334. ret = fscanf(fp, "%s", buffer);
  335. while(ret != 0 && ret != EOF) {
  336. strcat(arguments, buffer);
  337. strcat(arguments, " ");
  338. ret = fscanf(fp, "%s", buffer);
  339. }
  340. if (strlen(arguments) > 0) {
  341. good = 1;
  342. }
  343. }
  344. if (!good) {
  345. #if defined(__APPLE__) && defined(__POWERPC__)
  346. command = JACK_LOCATION "/jackd";
  347. strncpy(arguments, JACK_LOCATION "/jackd -T -d portaudio -p 512", 255);
  348. #elif defined(USE_CAPABILITIES)
  349. command = JACK_LOCATION "/jackstart";
  350. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d alsa -d hw:0 -p 512", 255);
  351. #else
  352. command = JACK_LOCATION "/jackd";
  353. strncpy(arguments, JACK_LOCATION "/jackd -T -d alsa -d hw:0 -p 2048", 255);
  354. #endif
  355. } else {
  356. result = strcspn(arguments, " ");
  357. command = (char*)malloc(result+1);
  358. strncpy(command, arguments, result);
  359. command[result] = '\0';
  360. }
  361. argv = (char**)malloc(255);
  362. while(1) {
  363. result = strcspn(arguments+pos, " ");
  364. if (result == 0) {
  365. break;
  366. }
  367. argv[i] = (char*)malloc(result+1);
  368. strncpy(argv[i], arguments+pos, result);
  369. argv[i][result] = '\0';
  370. pos += result+1;
  371. ++i;
  372. }
  373. execv (command, argv);
  374. }
  375. int
  376. start_server (void)
  377. {
  378. pid_t pid1, pid2;
  379. if (getenv("JACK_NO_START_SERVER")) {
  380. return 1;
  381. }
  382. switch (pid1 = fork()) {
  383. case -1:
  384. return 1;
  385. case 0:
  386. switch (pid2 = fork()) {
  387. case -1:
  388. return 1;
  389. case 0:
  390. _start_server();
  391. }
  392. _exit(0);
  393. }
  394. return 0;
  395. }
  396. static int
  397. jack_request_client (ClientType type, const char* client_name,
  398. const char* so_name, const char* so_data,
  399. jack_client_connect_result_t *res, int *req_fd)
  400. {
  401. jack_client_connect_request_t req;
  402. *req_fd = -1;
  403. memset (&req, 0, sizeof (req));
  404. if (strlen (client_name) >= sizeof (req.name)) {
  405. jack_error ("\"%s\" is too long to be used as a JACK client"
  406. " name.\n"
  407. "Please use %lu characters or less.",
  408. client_name, sizeof (req.name));
  409. return -1;
  410. }
  411. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  412. jack_error ("\"%s\" is too long to be used as a JACK shared"
  413. " object name.\n"
  414. "Please use %lu characters or less.",
  415. so_name, sizeof (req.object_path) - 1);
  416. return -1;
  417. }
  418. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  419. jack_error ("\"%s\" is too long to be used as a JACK shared"
  420. " object data string.\n"
  421. "Please use %lu characters or less.",
  422. so_data, sizeof (req.object_data) - 1);
  423. return -1;
  424. }
  425. if ((*req_fd = server_connect (0)) < 0) {
  426. if (start_server() == 0) {
  427. sleep(2);
  428. if ((*req_fd = server_connect (0)) < 0) {
  429. goto fail;
  430. }
  431. } else {
  432. goto fail;
  433. }
  434. }
  435. req.load = TRUE;
  436. req.type = type;
  437. snprintf (req.name, sizeof (req.name), "%s", client_name);
  438. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  439. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  440. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  441. jack_error ("cannot send request to jack server (%s)",
  442. strerror (errno));
  443. goto fail;
  444. }
  445. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  446. if (errno == 0) {
  447. /* server shut the socket */
  448. jack_error ("could not attach as client "
  449. "(duplicate client name?)");
  450. goto fail;
  451. }
  452. jack_error ("cannot read response from jack server (%s)",
  453. strerror (errno));
  454. goto fail;
  455. }
  456. if (res->status) {
  457. jack_error ("could not attach as client "
  458. "(duplicate client name?)");
  459. goto fail;
  460. }
  461. if (res->protocol_v != jack_protocol_version){
  462. jack_error ("application linked against incompatible libjack"
  463. " version.");
  464. goto fail;
  465. }
  466. switch (type) {
  467. case ClientDriver:
  468. case ClientInternal:
  469. close (*req_fd);
  470. *req_fd = -1;
  471. break;
  472. default:
  473. break;
  474. }
  475. return 0;
  476. fail:
  477. if (*req_fd >= 0) {
  478. close (*req_fd);
  479. *req_fd = -1;
  480. }
  481. return -1;
  482. }
  483. int
  484. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  485. {
  486. /* Lookup, attach and register the port/buffer segments in use
  487. * right now.
  488. */
  489. if (client->control->type != ClientExternal) {
  490. jack_error("Only external clients need attach port segments");
  491. abort();
  492. }
  493. /* make sure we have space to store the port
  494. segment information.
  495. */
  496. if (ptid >= client->n_port_types) {
  497. client->port_segment = (jack_shm_info_t*)
  498. realloc (client->port_segment,
  499. sizeof (jack_shm_info_t) * (ptid+1));
  500. memset (&client->port_segment[client->n_port_types],
  501. 0,
  502. sizeof (jack_shm_info_t) *
  503. (ptid - client->n_port_types));
  504. client->n_port_types = ptid + 1;
  505. } else {
  506. /* release any previous segment */
  507. jack_release_shm (&client->port_segment[ptid]);
  508. }
  509. /* get the index into the shm registry */
  510. client->port_segment[ptid].index =
  511. client->engine->port_types[ptid].shm_registry_index;
  512. /* attach the relevant segment */
  513. if (jack_attach_shm (&client->port_segment[ptid])) {
  514. jack_error ("cannot attach port segment shared memory"
  515. " (%s)", strerror (errno));
  516. return -1;
  517. }
  518. /* The first chunk of the audio port segment will be set by
  519. * the engine to be a zero-filled buffer. This hasn't been
  520. * done yet, but it will happen before the process cycle
  521. * (re)starts.
  522. */
  523. if (ptid == JACK_AUDIO_PORT_TYPE) {
  524. jack_zero_filled_buffer =
  525. jack_shm_addr (&client->port_segment[ptid]);
  526. }
  527. return 0;
  528. }
  529. jack_client_t *
  530. jack_client_new (const char *client_name)
  531. {
  532. int req_fd = -1;
  533. int ev_fd = -1;
  534. jack_client_connect_result_t res;
  535. jack_client_t *client;
  536. jack_port_type_id_t ptid;
  537. /* external clients need this initialized; internal clients
  538. will use the setup in the server's address space.
  539. */
  540. jack_init_time ();
  541. jack_initialize_shm ();
  542. if (jack_request_client (ClientExternal, client_name, "", "",
  543. &res, &req_fd)) {
  544. return NULL;
  545. }
  546. client = jack_client_alloc ();
  547. strcpy (client->fifo_prefix, res.fifo_prefix);
  548. client->request_fd = req_fd;
  549. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  550. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  551. /* attach the engine control/info block */
  552. client->engine_shm = res.engine_shm;
  553. if (jack_attach_shm (&client->engine_shm)) {
  554. jack_error ("cannot attached engine control shared memory"
  555. " segment");
  556. goto fail;
  557. }
  558. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  559. /* now attach the client control block */
  560. client->control_shm = res.client_shm;
  561. if (jack_attach_shm (&client->control_shm)) {
  562. jack_error ("cannot attached client control shared memory"
  563. " segment");
  564. goto fail;
  565. }
  566. client->control = (jack_client_control_t *)
  567. jack_shm_addr (&client->control_shm);
  568. /* nobody else needs to access this shared memory any more, so
  569. destroy it. because we have our own attachment to it, it won't
  570. vanish till we exit (and release it).
  571. */
  572. jack_destroy_shm (&client->control_shm);
  573. client->n_port_types = client->engine->n_port_types;
  574. client->port_segment = (jack_shm_info_t *)
  575. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  576. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  577. client->port_segment[ptid].index =
  578. client->engine->port_types[ptid].shm_registry_index;
  579. jack_attach_port_segment (client, ptid);
  580. }
  581. /* set up the client so that it does the right thing for an
  582. * external client
  583. */
  584. client->control->deliver_request = oop_client_deliver_request;
  585. client->control->deliver_arg = client;
  586. if ((ev_fd = server_event_connect (client)) < 0) {
  587. goto fail;
  588. }
  589. client->event_fd = ev_fd;
  590. #if defined(__APPLE__) && defined(__POWERPC__)
  591. /* specific resources for server/client real-time thread
  592. * communication */
  593. client->clienttask = mach_task_self();
  594. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  595. jack_error ("Can't find bootstrap port");
  596. goto fail;
  597. }
  598. if (allocate_mach_clientport(client, res.portnum) < 0) {
  599. jack_error("Can't allocate mach port");
  600. goto fail;
  601. };
  602. #endif
  603. return client;
  604. fail:
  605. if (client->engine) {
  606. jack_release_shm (&client->engine_shm);
  607. client->engine = 0;
  608. }
  609. if (client->control) {
  610. jack_release_shm (&client->control_shm);
  611. client->control = 0;
  612. }
  613. if (req_fd >= 0) {
  614. close (req_fd);
  615. }
  616. if (ev_fd >= 0) {
  617. close (ev_fd);
  618. }
  619. return 0;
  620. }
  621. int
  622. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  623. {
  624. jack_client_connect_result_t res;
  625. int req_fd;
  626. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  627. }
  628. void
  629. jack_internal_client_close (const char *client_name)
  630. {
  631. jack_client_connect_request_t req;
  632. int fd;
  633. req.load = FALSE;
  634. snprintf (req.name, sizeof (req.name), "%s", client_name);
  635. if ((fd = server_connect (0)) < 0) {
  636. return;
  637. }
  638. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  639. jack_error ("cannot deliver ClientUnload request to JACK server.");
  640. }
  641. /* no response to this request */
  642. close (fd);
  643. return;
  644. }
  645. int
  646. jack_drop_real_time_scheduling (pthread_t thread)
  647. {
  648. struct sched_param rtparam;
  649. int x;
  650. memset (&rtparam, 0, sizeof (rtparam));
  651. rtparam.sched_priority = 0;
  652. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  653. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  654. return -1;
  655. }
  656. return 0;
  657. }
  658. int
  659. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  660. {
  661. struct sched_param rtparam;
  662. int x;
  663. memset (&rtparam, 0, sizeof (rtparam));
  664. rtparam.sched_priority = priority;
  665. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  666. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  667. "(%d: %s)", rtparam.sched_priority, x,
  668. strerror (x));
  669. return -1;
  670. }
  671. return 0;
  672. }
  673. int
  674. jack_set_freewheel (jack_client_t* client, int onoff)
  675. {
  676. jack_request_t request;
  677. request.type = onoff ? FreeWheel : StopFreeWheel;
  678. return jack_client_deliver_request (client, &request);
  679. }
  680. void
  681. jack_start_freewheel (jack_client_t* client)
  682. {
  683. jack_client_control_t *control = client->control;
  684. if (client->engine->real_time) {
  685. jack_drop_real_time_scheduling (client->thread);
  686. }
  687. if (control->freewheel_cb) {
  688. control->freewheel_cb (1, control->freewheel_arg);
  689. }
  690. }
  691. void
  692. jack_stop_freewheel (jack_client_t* client)
  693. {
  694. jack_client_control_t *control = client->control;
  695. if (control->freewheel_cb) {
  696. control->freewheel_cb (0, control->freewheel_arg);
  697. }
  698. if (client->engine->real_time) {
  699. jack_acquire_real_time_scheduling (client->thread,
  700. client->engine->client_priority);
  701. }
  702. }
  703. static void *
  704. jack_client_thread (void *arg)
  705. {
  706. jack_client_t *client = (jack_client_t *) arg;
  707. jack_client_control_t *control = client->control;
  708. jack_event_t event;
  709. char status = 0;
  710. char c;
  711. int err = 0;
  712. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  713. pthread_mutex_lock (&client_lock);
  714. client->thread_ok = TRUE;
  715. client->thread_id = pthread_self();
  716. pthread_cond_signal (&client_ready);
  717. pthread_mutex_unlock (&client_lock);
  718. client->control->pid = getpid();
  719. client->control->pgrp = getpgrp();
  720. DEBUG ("client thread is now running");
  721. while (err == 0) {
  722. if (client->engine->engine_ok == 0) {
  723. if (client->on_shutdown)
  724. client->on_shutdown (client->on_shutdown_arg);
  725. else
  726. jack_error ("engine unexpectedly shutdown; "
  727. "thread exiting\n");
  728. pthread_exit (0);
  729. }
  730. DEBUG ("client polling on event_fd and graph_wait_fd...");
  731. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  732. if (errno == EINTR) {
  733. continue;
  734. }
  735. jack_error ("poll failed in client (%s)",
  736. strerror (errno));
  737. status = -1;
  738. break;
  739. }
  740. /* get an accurate timestamp on waking from poll for a
  741. * process() cycle.
  742. */
  743. if (client->pollfd[1].revents & POLLIN) {
  744. control->awake_at = jack_get_microseconds();
  745. }
  746. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  747. client->pollfd[0].revents,
  748. client->pollfd[1].revents);
  749. pthread_testcancel();
  750. if ((client->pollfd[0].revents & ~POLLIN) ||
  751. client->control->dead) {
  752. goto zombie;
  753. }
  754. if (client->pollfd[0].revents & POLLIN) {
  755. DEBUG ("client receives an event, "
  756. "now reading on event fd");
  757. /* server has sent us an event. process the
  758. * event and reply */
  759. if (read (client->event_fd, &event, sizeof (event))
  760. != sizeof (event)) {
  761. jack_error ("cannot read server event (%s)",
  762. strerror (errno));
  763. err++;
  764. break;
  765. }
  766. status = 0;
  767. switch (event.type) {
  768. case PortRegistered:
  769. if (control->port_register) {
  770. control->port_register
  771. (event.x.port_id, TRUE,
  772. control->port_register_arg);
  773. }
  774. break;
  775. case PortUnregistered:
  776. if (control->port_register) {
  777. control->port_register
  778. (event.x.port_id, FALSE,
  779. control->port_register_arg);
  780. }
  781. break;
  782. case GraphReordered:
  783. status = jack_handle_reorder (client, &event);
  784. break;
  785. case PortConnected:
  786. case PortDisconnected:
  787. status = jack_client_handle_port_connection
  788. (client, &event);
  789. break;
  790. case BufferSizeChange:
  791. jack_client_invalidate_port_buffers (client);
  792. if (control->bufsize) {
  793. status = control->bufsize
  794. (control->nframes,
  795. control->bufsize_arg);
  796. }
  797. break;
  798. case SampleRateChange:
  799. if (control->srate) {
  800. status = control->srate
  801. (control->nframes,
  802. control->srate_arg);
  803. }
  804. break;
  805. case XRun:
  806. if (control->xrun) {
  807. status = control->xrun
  808. (control->xrun_arg);
  809. }
  810. break;
  811. case AttachPortSegment:
  812. jack_attach_port_segment (client, event.y.ptid);
  813. break;
  814. case StartFreewheel:
  815. jack_start_freewheel (client);
  816. break;
  817. case StopFreewheel:
  818. jack_stop_freewheel (client);
  819. break;
  820. }
  821. DEBUG ("client has dealt with the event, writing "
  822. "response on event fd");
  823. if (write (client->event_fd, &status, sizeof (status))
  824. != sizeof (status)) {
  825. jack_error ("cannot send event response to "
  826. "engine (%s)", strerror (errno));
  827. err++;
  828. break;
  829. }
  830. }
  831. if (client->pollfd[1].revents & ~POLLIN) {
  832. goto zombie;
  833. }
  834. if (client->pollfd[1].revents & POLLIN) {
  835. #ifdef WITH_TIMESTAMPS
  836. jack_reset_timestamps ();
  837. #endif
  838. DEBUG ("client %d signalled at %" PRIu64
  839. ", awake for process at %" PRIu64
  840. " (delay = %" PRIu64
  841. " usecs) (wakeup on graph_wait_fd==%d)",
  842. getpid(),
  843. control->signalled_at,
  844. control->awake_at,
  845. control->awake_at - control->signalled_at,
  846. client->pollfd[1].fd);
  847. control->state = Running;
  848. if (control->sync_cb)
  849. jack_call_sync_client (client);
  850. if (control->process) {
  851. if (control->process (control->nframes,
  852. control->process_arg)
  853. == 0) {
  854. control->state = Finished;
  855. }
  856. } else {
  857. control->state = Finished;
  858. }
  859. if (control->timebase_cb)
  860. jack_call_timebase_master (client);
  861. control->finished_at = jack_get_microseconds();
  862. #ifdef WITH_TIMESTAMPS
  863. jack_timestamp ("finished");
  864. #endif
  865. /* pass the execution token along */
  866. DEBUG ("client finished processing at %" PRIu64
  867. " (elapsed = %" PRIu64
  868. " usecs), writing on graph_next_fd==%d",
  869. control->finished_at,
  870. control->finished_at - control->awake_at,
  871. client->graph_next_fd);
  872. if (write (client->graph_next_fd, &c, sizeof (c))
  873. != sizeof (c)) {
  874. jack_error ("cannot continue execution of the "
  875. "processing graph (%s)",
  876. strerror(errno));
  877. err++;
  878. break;
  879. }
  880. DEBUG ("client sent message to next stage by %" PRIu64
  881. ", client reading on graph_wait_fd==%d",
  882. jack_get_microseconds(), client->graph_wait_fd);
  883. #ifdef WITH_TIMESTAMPS
  884. jack_timestamp ("read pending byte from wait");
  885. #endif
  886. DEBUG("reading cleanup byte from pipe\n");
  887. if ((read (client->graph_wait_fd, &c, sizeof (c))
  888. != sizeof (c))) {
  889. DEBUG ("WARNING: READ FAILED!");
  890. #if 0
  891. jack_error ("cannot complete execution of the "
  892. "processing graph (%s)",
  893. strerror(errno));
  894. err++;
  895. break;
  896. #endif
  897. }
  898. /* check if we were killed during the process
  899. * cycle (or whatever) */
  900. if (client->control->dead) {
  901. goto zombie;
  902. }
  903. DEBUG("process cycle fully complete\n");
  904. #ifdef WITH_TIMESTAMPS
  905. jack_timestamp ("read done");
  906. jack_dump_timestamps (stdout);
  907. #endif
  908. }
  909. }
  910. return (void *) ((intptr_t)err);
  911. zombie:
  912. if (client->on_shutdown) {
  913. jack_error ("zombified - calling shutdown handler");
  914. client->on_shutdown (client->on_shutdown_arg);
  915. } else {
  916. jack_error ("zombified - exiting from JACK");
  917. jack_client_close (client);
  918. /* Need a fix : possibly make client crash if
  919. * zombified without shutdown handler
  920. */
  921. }
  922. pthread_exit (0);
  923. /*NOTREACHED*/
  924. return 0;
  925. }
  926. #if defined(__APPLE__) && defined(__POWERPC__)
  927. /* real-time thread : separated from the normal client thread, it will
  928. * communicate with the server using fast mach RPC mechanism */
  929. static void *
  930. jack_client_process_thread (void *arg)
  931. {
  932. jack_client_t *client = (jack_client_t *) arg;
  933. jack_client_control_t *control = client->control;
  934. int err = 0;
  935. client->control->pid = getpid();
  936. DEBUG ("client process thread is now running");
  937. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  938. while (err == 0) {
  939. if (jack_client_suspend(client) < 0) {
  940. pthread_exit (0);
  941. }
  942. control->awake_at = jack_get_microseconds();
  943. DEBUG ("client resumed");
  944. control->state = Running;
  945. if (control->process) {
  946. if (control->process (control->nframes,
  947. control->process_arg) == 0) {
  948. control->state = Finished;
  949. }
  950. } else {
  951. control->state = Finished;
  952. }
  953. control->finished_at = jack_get_microseconds();
  954. #ifdef WITH_TIMESTAMPS
  955. jack_timestamp ("finished");
  956. #endif
  957. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  958. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  959. /* check if we were killed during the process cycle (or whatever) */
  960. if (client->control->dead) {
  961. jack_error ("jack_client_process_thread : client->control->dead");
  962. goto zombie;
  963. }
  964. DEBUG("process cycle fully complete\n");
  965. }
  966. return (void *) ((intptr_t)err);
  967. zombie:
  968. jack_error ("jack_client_process_thread : zombified");
  969. if (client->on_shutdown) {
  970. jack_error ("zombified - calling shutdown handler");
  971. client->on_shutdown (client->on_shutdown_arg);
  972. } else {
  973. jack_error ("zombified - exiting from JACK");
  974. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  975. }
  976. pthread_exit (0);
  977. /*NOTREACHED*/
  978. return 0;
  979. }
  980. #endif
  981. static int
  982. jack_start_thread (jack_client_t *client)
  983. {
  984. pthread_attr_t *attributes = 0;
  985. #ifdef USE_CAPABILITIES
  986. int policy = SCHED_OTHER;
  987. struct sched_param client_param, temp_param;
  988. #endif
  989. if (client->engine->real_time) {
  990. /* Get the client thread to run as an RT-FIFO
  991. scheduled thread of appropriate priority.
  992. */
  993. struct sched_param rt_param;
  994. attributes = (pthread_attr_t *)
  995. malloc (sizeof (pthread_attr_t));
  996. pthread_attr_init (attributes);
  997. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  998. jack_error ("cannot set FIFO scheduling class for RT "
  999. "thread");
  1000. return -1;
  1001. }
  1002. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1003. jack_error ("Cannot set scheduling scope for RT "
  1004. "thread");
  1005. return -1;
  1006. }
  1007. memset (&rt_param, 0, sizeof (rt_param));
  1008. rt_param.sched_priority = client->engine->client_priority;
  1009. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  1010. jack_error ("Cannot set scheduling priority for RT "
  1011. "thread (%s)", strerror (errno));
  1012. return -1;
  1013. }
  1014. #if defined(__APPLE__) && defined(__POWERPC__)
  1015. // To be implemented
  1016. #else
  1017. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  1018. jack_error ("cannot lock down memory for RT thread (%s)",
  1019. strerror (errno));
  1020. #ifdef ENSURE_MLOCK
  1021. return -1;
  1022. #endif /* ENSURE_MLOCK */
  1023. }
  1024. #endif
  1025. }
  1026. if (pthread_create (&client->thread, attributes,
  1027. jack_client_thread, client)) {
  1028. #ifdef USE_CAPABILITIES
  1029. if (client->engine->real_time) {
  1030. /* we are probably dealing with a broken glibc so try
  1031. to work around the bug, see below for more details
  1032. */
  1033. goto capabilities_workaround;
  1034. }
  1035. #endif
  1036. return -1;
  1037. }
  1038. #if defined(__APPLE__) && defined(__POWERPC__)
  1039. /* a spcial real-time thread to call the "process"
  1040. * callback. It will communicate with the server using fast
  1041. * mach RPC mechanism */
  1042. if (pthread_create (&client->process_thread, attributes,
  1043. jack_client_process_thread, client)) {
  1044. jack_error("pthread_create failed for process_thread \n");
  1045. return -1;
  1046. }
  1047. if (client->engine->real_time){
  1048. /* time constraint thread */
  1049. setThreadToPriority(client->process_thread, 96, true, 10000000);
  1050. }else{
  1051. /* fixed priority thread */
  1052. setThreadToPriority(client->process_thread, 63, true, 10000000);
  1053. }
  1054. #endif
  1055. return 0;
  1056. #ifdef USE_CAPABILITIES
  1057. /* we get here only with engine running realtime and capabilities */
  1058. capabilities_workaround:
  1059. /* the version of glibc I've played with has a bug that makes
  1060. that code fail when running under a non-root user but with the
  1061. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  1062. does not check for capabilities, only for the uid being
  1063. zero). Newer versions apparently have this fixed. This
  1064. workaround temporarily switches the client thread to the
  1065. proper scheduler and priority, then starts the realtime
  1066. thread so that it can inherit them and finally switches the
  1067. client thread back to what it was before. Sigh. For ardour
  1068. I have to check again and switch the thread explicitly to
  1069. realtime, don't know why or how to debug - nando
  1070. */
  1071. /* get current scheduler and parameters of the client process */
  1072. if ((policy = sched_getscheduler (0)) < 0) {
  1073. jack_error ("Cannot get current client scheduler: %s",
  1074. strerror(errno));
  1075. return -1;
  1076. }
  1077. memset (&client_param, 0, sizeof (client_param));
  1078. if (sched_getparam (0, &client_param)) {
  1079. jack_error ("Cannot get current client scheduler "
  1080. "parameters: %s", strerror(errno));
  1081. return -1;
  1082. }
  1083. /* temporarily change the client process to SCHED_FIFO so that
  1084. the realtime thread can inherit the scheduler and priority
  1085. */
  1086. memset (&temp_param, 0, sizeof (temp_param));
  1087. temp_param.sched_priority = client->engine->client_priority;
  1088. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  1089. jack_error ("Cannot temporarily set client to RT scheduler:"
  1090. " %s", strerror(errno));
  1091. return -1;
  1092. }
  1093. /* prepare the attributes for the realtime thread */
  1094. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1095. pthread_attr_init (attributes);
  1096. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1097. sched_setscheduler (0, policy, &client_param);
  1098. jack_error ("Cannot set scheduling scope for RT thread");
  1099. return -1;
  1100. }
  1101. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1102. sched_setscheduler (0, policy, &client_param);
  1103. jack_error ("Cannot set scheduler inherit policy for RT "
  1104. "thread");
  1105. return -1;
  1106. }
  1107. /* create the RT thread */
  1108. if (pthread_create (&client->thread, attributes,
  1109. jack_client_thread, client)) {
  1110. sched_setscheduler (0, policy, &client_param);
  1111. return -1;
  1112. }
  1113. /* return the client process to the scheduler it was in before */
  1114. if (sched_setscheduler (0, policy, &client_param)) {
  1115. jack_error ("Cannot reset original client scheduler: %s",
  1116. strerror(errno));
  1117. return -1;
  1118. }
  1119. /* check again... inheritance of policy and priority works in
  1120. jack_simple_client but not in ardour! So I check again and
  1121. force the policy if it is not set correctly. This does not
  1122. really really work either, the manager thread of the
  1123. linuxthreads implementation is left running with
  1124. SCHED_OTHER, that is presumably very bad.
  1125. */
  1126. memset (&client_param, 0, sizeof (client_param));
  1127. if (pthread_getschedparam(client->thread, &policy,
  1128. &client_param) == 0) {
  1129. if (policy != SCHED_FIFO) {
  1130. memset (&client_param, 0, sizeof (client_param));
  1131. client_param.sched_priority =
  1132. client->engine->client_priority;
  1133. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1134. &client_param)) {
  1135. jack_error ("Cannot set (again) FIFO scheduling"
  1136. " class for RT thread\n");
  1137. return -1;
  1138. }
  1139. }
  1140. }
  1141. return 0;
  1142. #endif
  1143. }
  1144. int
  1145. jack_activate (jack_client_t *client)
  1146. {
  1147. jack_request_t req;
  1148. /* we need to scribble on our stack to ensure that its memory
  1149. * pages are actually mapped (more important for mlockall(2)
  1150. * usage in jack_start_thread())
  1151. */
  1152. #if defined(__APPLE__) && defined(__POWERPC__)
  1153. /* a bigger stack makes the application crash... */
  1154. #define BIG_ENOUGH_STACK 10000
  1155. #else
  1156. #define BIG_ENOUGH_STACK 1048576
  1157. #endif
  1158. char buf[BIG_ENOUGH_STACK];
  1159. int i;
  1160. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  1161. buf[i] = (char) (i & 0xff);
  1162. }
  1163. #undef BIG_ENOUGH_STACK
  1164. if (client->control->type == ClientInternal ||
  1165. client->control->type == ClientDriver) {
  1166. goto startit;
  1167. }
  1168. /* get the pid of the client process to pass it to engine */
  1169. client->control->pid = getpid ();
  1170. #ifdef USE_CAPABILITIES
  1171. if (client->engine->has_capabilities != 0 &&
  1172. client->control->pid != 0 && client->engine->real_time != 0) {
  1173. /* we need to ask the engine for realtime capabilities
  1174. before trying to start the realtime thread
  1175. */
  1176. req.type = SetClientCapabilities;
  1177. req.x.client_id = client->control->id;
  1178. jack_client_deliver_request (client, &req);
  1179. if (req.status) {
  1180. /* what to do? engine is running realtime, it
  1181. is using capabilities and has them
  1182. (otherwise we would not get an error
  1183. return) but for some reason it could not
  1184. give the client the required capabilities,
  1185. so for now downgrade the client so that it
  1186. still runs, albeit non-realtime - nando
  1187. */
  1188. jack_error ("could not receive realtime capabilities, "
  1189. "client will run non-realtime");
  1190. /* XXX wrong, this is a property of the engine
  1191. client->engine->real_time = 0;
  1192. */
  1193. }
  1194. }
  1195. #endif
  1196. if (client->first_active) {
  1197. pthread_mutex_init (&client_lock, NULL);
  1198. pthread_cond_init (&client_ready, NULL);
  1199. pthread_mutex_lock (&client_lock);
  1200. if (jack_start_thread (client)) {
  1201. pthread_mutex_unlock (&client_lock);
  1202. return -1;
  1203. }
  1204. pthread_cond_wait (&client_ready, &client_lock);
  1205. pthread_mutex_unlock (&client_lock);
  1206. if (!client->thread_ok) {
  1207. jack_error ("could not start client thread");
  1208. return -1;
  1209. }
  1210. client->first_active = FALSE;
  1211. }
  1212. startit:
  1213. req.type = ActivateClient;
  1214. req.x.client_id = client->control->id;
  1215. return jack_client_deliver_request (client, &req);
  1216. }
  1217. int
  1218. jack_deactivate (jack_client_t *client)
  1219. {
  1220. jack_request_t req;
  1221. req.type = DeactivateClient;
  1222. req.x.client_id = client->control->id;
  1223. return jack_client_deliver_request (client, &req);
  1224. }
  1225. int
  1226. jack_client_close (jack_client_t *client)
  1227. {
  1228. JSList *node;
  1229. void *status;
  1230. if (client->control->active) {
  1231. jack_deactivate (client);
  1232. }
  1233. if (client->control->type == ClientExternal) {
  1234. /* stop the thread that communicates with the jack
  1235. * server, only if it was actually running
  1236. */
  1237. if (client->thread_ok){
  1238. pthread_cancel (client->thread);
  1239. pthread_join (client->thread, &status);
  1240. }
  1241. if (client->control) {
  1242. jack_release_shm (&client->control_shm);
  1243. client->control = NULL;
  1244. }
  1245. if (client->engine) {
  1246. jack_release_shm (&client->engine_shm);
  1247. client->engine = NULL;
  1248. }
  1249. if (client->port_segment) {
  1250. jack_port_type_id_t ptid;
  1251. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1252. jack_release_shm (&client->port_segment[ptid]);
  1253. }
  1254. free (client->port_segment);
  1255. client->port_segment = NULL;
  1256. }
  1257. if (client->graph_wait_fd) {
  1258. close (client->graph_wait_fd);
  1259. }
  1260. if (client->graph_next_fd) {
  1261. close (client->graph_next_fd);
  1262. }
  1263. close (client->event_fd);
  1264. close (client->request_fd);
  1265. }
  1266. for (node = client->ports; node; node = jack_slist_next (node)) {
  1267. free (node->data);
  1268. }
  1269. jack_slist_free (client->ports);
  1270. jack_client_free (client);
  1271. return 0;
  1272. }
  1273. int
  1274. jack_is_realtime (jack_client_t *client)
  1275. {
  1276. return client->engine->real_time;
  1277. }
  1278. jack_nframes_t
  1279. jack_get_buffer_size (jack_client_t *client)
  1280. {
  1281. return client->engine->buffer_size;
  1282. }
  1283. int
  1284. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1285. {
  1286. #ifdef DO_BUFFER_RESIZE
  1287. jack_request_t req;
  1288. req.type = SetBufferSize;
  1289. req.x.nframes = nframes;
  1290. return jack_client_deliver_request (client, &req);
  1291. #else
  1292. return ENOSYS;
  1293. #endif /* DO_BUFFER_RESIZE */
  1294. }
  1295. int
  1296. jack_connect (jack_client_t *client, const char *source_port,
  1297. const char *destination_port)
  1298. {
  1299. jack_request_t req;
  1300. req.type = ConnectPorts;
  1301. snprintf (req.x.connect.source_port,
  1302. sizeof (req.x.connect.source_port), "%s", source_port);
  1303. snprintf (req.x.connect.destination_port,
  1304. sizeof (req.x.connect.destination_port),
  1305. "%s", destination_port);
  1306. return jack_client_deliver_request (client, &req);
  1307. }
  1308. int
  1309. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1310. {
  1311. jack_request_t req;
  1312. pthread_mutex_lock (&port->connection_lock);
  1313. if (port->connections == NULL) {
  1314. pthread_mutex_unlock (&port->connection_lock);
  1315. return 0;
  1316. }
  1317. pthread_mutex_unlock (&port->connection_lock);
  1318. req.type = DisconnectPort;
  1319. req.x.port_info.port_id = port->shared->id;
  1320. return jack_client_deliver_request (client, &req);
  1321. }
  1322. int
  1323. jack_disconnect (jack_client_t *client, const char *source_port,
  1324. const char *destination_port)
  1325. {
  1326. jack_request_t req;
  1327. req.type = DisconnectPorts;
  1328. snprintf (req.x.connect.source_port,
  1329. sizeof (req.x.connect.source_port), "%s", source_port);
  1330. snprintf (req.x.connect.destination_port,
  1331. sizeof (req.x.connect.destination_port),
  1332. "%s", destination_port);
  1333. return jack_client_deliver_request (client, &req);
  1334. }
  1335. void
  1336. jack_set_error_function (void (*func) (const char *))
  1337. {
  1338. jack_error_callback = func;
  1339. }
  1340. int
  1341. jack_set_graph_order_callback (jack_client_t *client,
  1342. JackGraphOrderCallback callback, void *arg)
  1343. {
  1344. if (client->control->active) {
  1345. jack_error ("You cannot set callbacks on an active client.");
  1346. return -1;
  1347. }
  1348. client->control->graph_order = callback;
  1349. client->control->graph_order_arg = arg;
  1350. return 0;
  1351. }
  1352. int jack_set_xrun_callback (jack_client_t *client,
  1353. JackXRunCallback callback, void *arg)
  1354. {
  1355. if (client->control->active) {
  1356. jack_error ("You cannot set callbacks on an active client.");
  1357. return -1;
  1358. }
  1359. client->control->xrun = callback;
  1360. client->control->xrun_arg = arg;
  1361. return 0;
  1362. }
  1363. int
  1364. jack_set_process_callback (jack_client_t *client,
  1365. JackProcessCallback callback, void *arg)
  1366. {
  1367. if (client->control->active) {
  1368. jack_error ("You cannot set callbacks on an active client.");
  1369. return -1;
  1370. }
  1371. client->control->process_arg = arg;
  1372. client->control->process = callback;
  1373. return 0;
  1374. }
  1375. int
  1376. jack_set_freewheel_callback (jack_client_t *client,
  1377. JackFreewheelCallback callback, void *arg)
  1378. {
  1379. if (client->control->active) {
  1380. jack_error ("You cannot set callbacks on an active client.");
  1381. return -1;
  1382. }
  1383. client->control->freewheel_arg = arg;
  1384. client->control->freewheel_cb = callback;
  1385. return 0;
  1386. }
  1387. int
  1388. jack_set_buffer_size_callback (jack_client_t *client,
  1389. JackBufferSizeCallback callback, void *arg)
  1390. {
  1391. client->control->bufsize_arg = arg;
  1392. client->control->bufsize = callback;
  1393. return 0;
  1394. }
  1395. int
  1396. jack_set_port_registration_callback(jack_client_t *client,
  1397. JackPortRegistrationCallback callback,
  1398. void *arg)
  1399. {
  1400. if (client->control->active) {
  1401. jack_error ("You cannot set callbacks on an active client.");
  1402. return -1;
  1403. }
  1404. client->control->port_register_arg = arg;
  1405. client->control->port_register = callback;
  1406. return 0;
  1407. }
  1408. int
  1409. jack_get_process_start_fd (jack_client_t *client)
  1410. {
  1411. /* once this has been called, the client thread
  1412. does not sleep on the graph wait fd.
  1413. */
  1414. client->pollmax = 1;
  1415. return client->graph_wait_fd;
  1416. }
  1417. int
  1418. jack_get_process_done_fd (jack_client_t *client)
  1419. {
  1420. return client->graph_next_fd;
  1421. }
  1422. void
  1423. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1424. {
  1425. client->on_shutdown = function;
  1426. client->on_shutdown_arg = arg;
  1427. }
  1428. const char **
  1429. jack_get_ports (jack_client_t *client,
  1430. const char *port_name_pattern,
  1431. const char *type_name_pattern,
  1432. unsigned long flags)
  1433. {
  1434. jack_control_t *engine;
  1435. const char **matching_ports;
  1436. unsigned long match_cnt;
  1437. jack_port_shared_t *psp;
  1438. unsigned long i;
  1439. regex_t port_regex;
  1440. regex_t type_regex;
  1441. int matching;
  1442. engine = client->engine;
  1443. if (port_name_pattern && port_name_pattern[0]) {
  1444. regcomp (&port_regex, port_name_pattern,
  1445. REG_EXTENDED|REG_NOSUB);
  1446. }
  1447. if (type_name_pattern && type_name_pattern[0]) {
  1448. regcomp (&type_regex, type_name_pattern,
  1449. REG_EXTENDED|REG_NOSUB);
  1450. }
  1451. psp = engine->ports;
  1452. match_cnt = 0;
  1453. matching_ports = (const char **)
  1454. malloc (sizeof (char *) * engine->port_max);
  1455. for (i = 0; i < engine->port_max; i++) {
  1456. matching = 1;
  1457. if (!psp[i].in_use) {
  1458. continue;
  1459. }
  1460. if (flags) {
  1461. if ((psp[i].flags & flags) != flags) {
  1462. matching = 0;
  1463. }
  1464. }
  1465. if (matching && port_name_pattern && port_name_pattern[0]) {
  1466. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1467. matching = 0;
  1468. }
  1469. }
  1470. if (matching && type_name_pattern && type_name_pattern[0]) {
  1471. jack_port_type_id_t ptid = psp[i].ptype_id;
  1472. if (regexec (&type_regex,
  1473. engine->port_types[ptid].type_name,
  1474. 0, NULL, 0)) {
  1475. matching = 0;
  1476. }
  1477. }
  1478. if (matching) {
  1479. matching_ports[match_cnt++] = psp[i].name;
  1480. }
  1481. }
  1482. matching_ports[match_cnt] = 0;
  1483. if (match_cnt == 0) {
  1484. free (matching_ports);
  1485. matching_ports = 0;
  1486. }
  1487. return matching_ports;
  1488. }
  1489. float
  1490. jack_cpu_load (jack_client_t *client)
  1491. {
  1492. return client->engine->cpu_load;
  1493. }
  1494. pthread_t
  1495. jack_client_thread_id (jack_client_t *client)
  1496. {
  1497. return client->thread_id;
  1498. }
  1499. int
  1500. jack_client_name_size(void)
  1501. {
  1502. return JACK_CLIENT_NAME_SIZE;
  1503. }
  1504. int
  1505. jack_port_name_size(void)
  1506. {
  1507. return JACK_PORT_NAME_SIZE;
  1508. }
  1509. int
  1510. jack_port_type_size(void)
  1511. {
  1512. return JACK_PORT_TYPE_SIZE;
  1513. }
  1514. #if defined(__APPLE__) && defined(__POWERPC__)
  1515. double __jack_time_ratio;
  1516. void jack_init_time ()
  1517. {
  1518. mach_timebase_info_data_t info;
  1519. mach_timebase_info(&info);
  1520. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1521. }
  1522. #else
  1523. jack_time_t
  1524. jack_get_mhz (void)
  1525. {
  1526. FILE *f = fopen("/proc/cpuinfo", "r");
  1527. if (f == 0)
  1528. {
  1529. perror("can't open /proc/cpuinfo\n");
  1530. exit(1);
  1531. }
  1532. for ( ; ; )
  1533. {
  1534. jack_time_t mhz;
  1535. int ret;
  1536. char buf[1000];
  1537. if (fgets(buf, sizeof(buf), f) == NULL) {
  1538. jack_error ("FATAL: cannot locate cpu MHz in "
  1539. "/proc/cpuinfo\n");
  1540. exit(1);
  1541. }
  1542. #if defined(__powerpc__)
  1543. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  1544. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  1545. defined(__x86_64__)
  1546. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  1547. #elif defined( __sparc__ )
  1548. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  1549. #elif defined( __mc68000__ )
  1550. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  1551. #elif defined( __s390__ )
  1552. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  1553. #else /* MIPS, ARM, alpha */
  1554. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  1555. #endif
  1556. if (ret == 1)
  1557. {
  1558. fclose(f);
  1559. return (jack_time_t)mhz;
  1560. }
  1561. }
  1562. }
  1563. jack_time_t __jack_cpu_mhz;
  1564. void jack_init_time ()
  1565. {
  1566. __jack_cpu_mhz = jack_get_mhz ();
  1567. }
  1568. #endif