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.

1830 lines
42KB

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