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.

1849 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. #include <sys/mman.h>
  32. #include <jack/internal.h>
  33. #include <jack/jack.h>
  34. #include <jack/engine.h>
  35. #include <jack/pool.h>
  36. #include <jack/jslist.h>
  37. #include <jack/version.h>
  38. #include <jack/shm.h>
  39. #include <jack/unlock.h>
  40. #include <jack/thread.h>
  41. #include <sysdeps/time.h>
  42. JACK_TIME_GLOBAL_DECL; /* One instance per process. */
  43. #include "local.h"
  44. #include <sysdeps/poll.h>
  45. #include <sysdeps/ipc.h>
  46. #ifdef JACK_USE_MACH_THREADS
  47. #include <sysdeps/pThreadUtilities.h>
  48. #endif
  49. #ifdef WITH_TIMESTAMPS
  50. #include <jack/timestamps.h>
  51. #endif /* WITH_TIMESTAMPS */
  52. char *jack_server_dir = DEFAULT_TMP_DIR;
  53. void
  54. jack_set_server_dir (const char *path)
  55. {
  56. jack_error ("jack_set_server_dir() is deprecated.\n"
  57. "Please contact the program's author");
  58. jack_server_dir = strdup (path);
  59. }
  60. static pthread_mutex_t client_lock;
  61. static pthread_cond_t client_ready;
  62. void *jack_zero_filled_buffer = NULL;
  63. #define EVENT_POLL_INDEX 0
  64. #define WAIT_POLL_INDEX 1
  65. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  66. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].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->upstream_is_jackd = 0;
  135. client->graph_wait_fd = -1;
  136. client->graph_next_fd = -1;
  137. client->ports = NULL;
  138. client->engine = NULL;
  139. client->control = NULL;
  140. client->thread_ok = FALSE;
  141. #if JACK_USE_MACH_THREADS
  142. client->rt_thread_ok = FALSE;
  143. #endif
  144. client->first_active = TRUE;
  145. client->on_shutdown = NULL;
  146. client->n_port_types = 0;
  147. client->port_segment = NULL;
  148. return client;
  149. }
  150. /*
  151. * Build the jack_client_t structure for an internal client.
  152. */
  153. jack_client_t *
  154. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  155. {
  156. jack_client_t* client;
  157. client = jack_client_alloc ();
  158. client->control = cc;
  159. client->engine = engine->control;
  160. client->n_port_types = client->engine->n_port_types;
  161. client->port_segment = &engine->port_segment[0];
  162. return client;
  163. }
  164. static void
  165. jack_client_free (jack_client_t *client)
  166. {
  167. if (client->pollfd) {
  168. free (client->pollfd);
  169. }
  170. free (client);
  171. }
  172. void
  173. jack_client_invalidate_port_buffers (jack_client_t *client)
  174. {
  175. JSList *node;
  176. jack_port_t *port;
  177. /* This releases all local memory owned by input ports
  178. and sets the buffer pointer to NULL. This will cause
  179. jack_port_get_buffer() to reallocate space for the
  180. buffer on the next call (if there is one).
  181. */
  182. for (node = client->ports; node; node = jack_slist_next (node)) {
  183. port = (jack_port_t *) node->data;
  184. if (port->shared->flags & JackPortIsInput) {
  185. if (port->mix_buffer) {
  186. jack_pool_release (port->mix_buffer);
  187. port->mix_buffer = NULL;
  188. }
  189. }
  190. }
  191. }
  192. int
  193. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  194. {
  195. jack_port_t *control_port;
  196. jack_port_t *other;
  197. JSList *node;
  198. switch (event->type) {
  199. case PortConnected:
  200. other = jack_port_new (client, event->y.other_id, client->engine);
  201. control_port = jack_port_by_id (client, event->x.self_id);
  202. pthread_mutex_lock (&control_port->connection_lock);
  203. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  204. pthread_mutex_unlock (&control_port->connection_lock);
  205. break;
  206. case PortDisconnected:
  207. control_port = jack_port_by_id (client, event->x.self_id);
  208. pthread_mutex_lock (&control_port->connection_lock);
  209. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  210. other = (jack_port_t *) node->data;
  211. if (other->shared->id == event->y.other_id) {
  212. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  213. jack_slist_free_1 (node);
  214. free (other);
  215. break;
  216. }
  217. }
  218. pthread_mutex_unlock (&control_port->connection_lock);
  219. break;
  220. default:
  221. /* impossible */
  222. break;
  223. }
  224. return 0;
  225. }
  226. static int
  227. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  228. {
  229. char path[PATH_MAX+1];
  230. if (client->graph_wait_fd >= 0) {
  231. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  232. close (client->graph_wait_fd);
  233. client->graph_wait_fd = -1;
  234. }
  235. if (client->graph_next_fd >= 0) {
  236. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  237. close (client->graph_next_fd);
  238. client->graph_next_fd = -1;
  239. }
  240. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  241. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  242. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  243. return -1;
  244. }
  245. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  246. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  247. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  248. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  249. return -1;
  250. }
  251. client->upstream_is_jackd = event->y.n;
  252. client->pollmax = 2;
  253. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)", client->graph_next_fd, path,
  254. client->upstream_is_jackd);
  255. /* If the client registered its own callback for graph order events,
  256. execute it now.
  257. */
  258. if (client->control->graph_order) {
  259. client->control->graph_order (client->control->graph_order_arg);
  260. }
  261. return 0;
  262. }
  263. static int
  264. server_connect (int which)
  265. {
  266. int fd;
  267. struct sockaddr_un addr;
  268. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  269. jack_error ("cannot create client socket (%s)",
  270. strerror (errno));
  271. return -1;
  272. }
  273. addr.sun_family = AF_UNIX;
  274. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d_%d",
  275. jack_server_dir, getuid (), which);
  276. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  277. close (fd);
  278. return -1;
  279. }
  280. return fd;
  281. }
  282. static int
  283. server_event_connect (jack_client_t *client)
  284. {
  285. int fd;
  286. struct sockaddr_un addr;
  287. jack_client_connect_ack_request_t req;
  288. jack_client_connect_ack_result_t res;
  289. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  290. jack_error ("cannot create client event socket (%s)",
  291. strerror (errno));
  292. return -1;
  293. }
  294. addr.sun_family = AF_UNIX;
  295. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d_ack_0",
  296. jack_server_dir, getuid () );
  297. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  298. jack_error ("cannot connect to jack server for events",
  299. strerror (errno));
  300. close (fd);
  301. return -1;
  302. }
  303. req.client_id = client->control->id;
  304. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  305. jack_error ("cannot write event connect request to server (%s)",
  306. strerror (errno));
  307. close (fd);
  308. return -1;
  309. }
  310. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  311. jack_error ("cannot read event connect result from server (%s)",
  312. strerror (errno));
  313. close (fd);
  314. return -1;
  315. }
  316. if (res.status != 0) {
  317. jack_error ("cannot connect to server for event stream (%s)",
  318. strerror (errno));
  319. close (fd);
  320. return -1;
  321. }
  322. return fd;
  323. }
  324. /* Exec the JACK server in this process. Does not return. */
  325. static void
  326. _start_server (void)
  327. {
  328. FILE* fp = 0;
  329. char filename[255];
  330. char arguments[255];
  331. char buffer[255];
  332. char* command = 0;
  333. size_t pos = 0;
  334. size_t result = 0;
  335. char** argv = 0;
  336. int i = 0;
  337. int good = 0;
  338. int ret;
  339. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  340. fp = fopen(filename, "r");
  341. if (!fp) {
  342. fp = fopen("/etc/jackd.conf", "r");
  343. }
  344. if (fp) {
  345. arguments[0] = '\0';
  346. ret = fscanf(fp, "%s", buffer);
  347. while(ret != 0 && ret != EOF) {
  348. strcat(arguments, buffer);
  349. strcat(arguments, " ");
  350. ret = fscanf(fp, "%s", buffer);
  351. }
  352. if (strlen(arguments) > 0) {
  353. good = 1;
  354. }
  355. }
  356. if (!good) {
  357. #if defined(USE_CAPABILITIES)
  358. command = JACK_LOCATION "/jackstart";
  359. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  360. JACK_DEFAULT_DRIVER " -p 512", 255);
  361. #else /* !USE_CAPABILITIES */
  362. command = JACK_LOCATION "/jackd";
  363. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  364. JACK_DEFAULT_DRIVER, 255);
  365. #endif /* USE_CAPABILITIES */
  366. } else {
  367. result = strcspn(arguments, " ");
  368. command = (char*)malloc(result+1);
  369. strncpy(command, arguments, result);
  370. command[result] = '\0';
  371. }
  372. argv = (char**)malloc(255);
  373. while(1) {
  374. /* insert -T into arguments */
  375. if (i == 1) {
  376. argv[i] = (char*)malloc(3);
  377. strncpy(argv[i], "-T", 2);
  378. argv[i][2] = '\0';
  379. ++i;
  380. }
  381. result = strcspn(arguments+pos, " ");
  382. if (result == 0) {
  383. break;
  384. }
  385. argv[i] = (char*)malloc(result+1);
  386. strncpy(argv[i], arguments+pos, result);
  387. argv[i][result] = '\0';
  388. pos += result+1;
  389. ++i;
  390. }
  391. argv[i] = 0;
  392. execv (command, argv);
  393. /* If execv() succeeds, it does not return. There's no point
  394. * in calling jack_error() here in the child process. */
  395. perror ("exec of JACK server failed");
  396. }
  397. int
  398. start_server (void)
  399. {
  400. /* Only fork() a server when $JACK_START_SERVER is defined and
  401. * $JACK_NO_START_SERVER is not. */
  402. if (getenv("JACK_START_SERVER") == NULL ||
  403. getenv("JACK_NO_START_SERVER") != NULL) {
  404. return 1;
  405. }
  406. /* The double fork() forces the server to become a child of
  407. * init, which will always clean up zombie process state on
  408. * termination. This even works in strange corner cases where
  409. * the server terminates but this client does not.
  410. *
  411. * Since fork() is usually implemented using copy-on-write
  412. * virtual memory tricks, the overhead of the second fork() is
  413. * probably relatively small.
  414. */
  415. switch (fork()) {
  416. case 0: /* child process */
  417. switch (fork()) {
  418. case 0: /* grandchild process */
  419. _start_server();
  420. _exit (99); /* exec failed */
  421. case -1:
  422. _exit (98);
  423. default:
  424. _exit (0);
  425. }
  426. case -1: /* fork() error */
  427. return 1; /* failed to start server */
  428. }
  429. /* only the original parent process goes here */
  430. return 0; /* (probably) successful */
  431. }
  432. static int
  433. jack_request_client (ClientType type, const char* client_name,
  434. const char* so_name, const char* so_data,
  435. jack_client_connect_result_t *res, int *req_fd)
  436. {
  437. jack_client_connect_request_t req;
  438. *req_fd = -1;
  439. memset (&req, 0, sizeof (req));
  440. if (strlen (client_name) >= sizeof (req.name)) {
  441. jack_error ("\"%s\" is too long to be used as a JACK client"
  442. " name.\n"
  443. "Please use %lu characters or less.",
  444. client_name, sizeof (req.name));
  445. return -1;
  446. }
  447. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  448. jack_error ("\"%s\" is too long to be used as a JACK shared"
  449. " object name.\n"
  450. "Please use %lu characters or less.",
  451. so_name, sizeof (req.object_path) - 1);
  452. return -1;
  453. }
  454. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  455. jack_error ("\"%s\" is too long to be used as a JACK shared"
  456. " object data string.\n"
  457. "Please use %lu characters or less.",
  458. so_data, sizeof (req.object_data) - 1);
  459. return -1;
  460. }
  461. if ((*req_fd = server_connect (0)) < 0) {
  462. int trys;
  463. if (start_server()) {
  464. goto fail;
  465. }
  466. trys = 5;
  467. do {
  468. sleep(1);
  469. if (--trys < 0) {
  470. goto fail;
  471. }
  472. } while ((*req_fd = server_connect (0)) < 0);
  473. }
  474. req.load = TRUE;
  475. req.type = type;
  476. snprintf (req.name, sizeof (req.name), "%s", client_name);
  477. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  478. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  479. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  480. jack_error ("cannot send request to jack server (%s)",
  481. strerror (errno));
  482. goto fail;
  483. }
  484. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  485. if (errno == 0) {
  486. /* server shut the socket */
  487. jack_error ("could not attach as client "
  488. "(duplicate client name?)");
  489. goto fail;
  490. }
  491. jack_error ("cannot read response from jack server (%s)",
  492. strerror (errno));
  493. goto fail;
  494. }
  495. if (res->status) {
  496. jack_error ("could not attach as client "
  497. "(duplicate client name?)");
  498. goto fail;
  499. }
  500. if (res->protocol_v != jack_protocol_version){
  501. jack_error ("application linked against incompatible libjack"
  502. " version.");
  503. goto fail;
  504. }
  505. switch (type) {
  506. case ClientDriver:
  507. case ClientInternal:
  508. close (*req_fd);
  509. *req_fd = -1;
  510. break;
  511. default:
  512. break;
  513. }
  514. return 0;
  515. fail:
  516. if (*req_fd >= 0) {
  517. close (*req_fd);
  518. *req_fd = -1;
  519. }
  520. return -1;
  521. }
  522. int
  523. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  524. {
  525. /* Lookup, attach and register the port/buffer segments in use
  526. * right now.
  527. */
  528. if (client->control->type != ClientExternal) {
  529. jack_error("Only external clients need attach port segments");
  530. abort();
  531. }
  532. /* make sure we have space to store the port
  533. segment information.
  534. */
  535. if (ptid >= client->n_port_types) {
  536. client->port_segment = (jack_shm_info_t*)
  537. realloc (client->port_segment,
  538. sizeof (jack_shm_info_t) * (ptid+1));
  539. memset (&client->port_segment[client->n_port_types],
  540. 0,
  541. sizeof (jack_shm_info_t) *
  542. (ptid - client->n_port_types));
  543. client->n_port_types = ptid + 1;
  544. } else {
  545. /* release any previous segment */
  546. #if JACK_USE_MACH_THREADS
  547. /* Stephane Letz : letz@grame.fr
  548. Need a fix : this crash on MacOSX : temporary removed
  549. jack_release_shm (&client->port_segment[ptid]);
  550. */
  551. #else
  552. jack_release_shm (&client->port_segment[ptid]);
  553. #endif
  554. }
  555. /* get the index into the shm registry */
  556. client->port_segment[ptid].index =
  557. client->engine->port_types[ptid].shm_registry_index;
  558. /* attach the relevant segment */
  559. if (jack_attach_shm (&client->port_segment[ptid])) {
  560. jack_error ("cannot attach port segment shared memory"
  561. " (%s)", strerror (errno));
  562. return -1;
  563. }
  564. /* The first chunk of the audio port segment will be set by
  565. * the engine to be a zero-filled buffer. This hasn't been
  566. * done yet, but it will happen before the process cycle
  567. * (re)starts.
  568. */
  569. if (ptid == JACK_AUDIO_PORT_TYPE) {
  570. jack_zero_filled_buffer =
  571. jack_shm_addr (&client->port_segment[ptid]);
  572. }
  573. return 0;
  574. }
  575. jack_client_t *
  576. jack_client_new (const char *client_name)
  577. {
  578. int req_fd = -1;
  579. int ev_fd = -1;
  580. jack_client_connect_result_t res;
  581. jack_client_t *client;
  582. jack_port_type_id_t ptid;
  583. /* external clients need this initialized; internal clients
  584. will use the setup in the server's address space.
  585. */
  586. jack_init_time ();
  587. if (jack_initialize_shm ()) {
  588. jack_error ("Unable to initialize shared memory.");
  589. return NULL;
  590. }
  591. if (jack_request_client (ClientExternal, client_name, "", "",
  592. &res, &req_fd)) {
  593. return NULL;
  594. }
  595. client = jack_client_alloc ();
  596. strcpy (client->fifo_prefix, res.fifo_prefix);
  597. client->request_fd = req_fd;
  598. client->pollfd[EVENT_POLL_INDEX].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  599. client->pollfd[WAIT_POLL_INDEX].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  600. /* attach the engine control/info block */
  601. client->engine_shm = res.engine_shm;
  602. if (jack_attach_shm (&client->engine_shm)) {
  603. jack_error ("cannot attached engine control shared memory"
  604. " segment");
  605. goto fail;
  606. }
  607. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  608. /* now attach the client control block */
  609. client->control_shm = res.client_shm;
  610. if (jack_attach_shm (&client->control_shm)) {
  611. jack_error ("cannot attached client control shared memory"
  612. " segment");
  613. goto fail;
  614. }
  615. client->control = (jack_client_control_t *)
  616. jack_shm_addr (&client->control_shm);
  617. /* nobody else needs to access this shared memory any more, so
  618. destroy it. because we have our own attachment to it, it won't
  619. vanish till we exit (and release it).
  620. */
  621. jack_destroy_shm (&client->control_shm);
  622. client->n_port_types = client->engine->n_port_types;
  623. client->port_segment = (jack_shm_info_t *)
  624. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  625. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  626. client->port_segment[ptid].index =
  627. client->engine->port_types[ptid].shm_registry_index;
  628. client->port_segment[ptid].attached_at = MAP_FAILED;
  629. jack_attach_port_segment (client, ptid);
  630. }
  631. /* set up the client so that it does the right thing for an
  632. * external client
  633. */
  634. client->control->deliver_request = oop_client_deliver_request;
  635. client->control->deliver_arg = client;
  636. if ((ev_fd = server_event_connect (client)) < 0) {
  637. goto fail;
  638. }
  639. client->event_fd = ev_fd;
  640. #ifdef JACK_USE_MACH_THREADS
  641. /* specific resources for server/client real-time thread
  642. * communication */
  643. client->clienttask = mach_task_self();
  644. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  645. jack_error ("Can't find bootstrap port");
  646. goto fail;
  647. }
  648. if (allocate_mach_clientport(client, res.portnum) < 0) {
  649. jack_error("Can't allocate mach port");
  650. goto fail;
  651. };
  652. #endif /* JACK_USE_MACH_THREADS */
  653. return client;
  654. fail:
  655. if (client->engine) {
  656. jack_release_shm (&client->engine_shm);
  657. client->engine = 0;
  658. }
  659. if (client->control) {
  660. jack_release_shm (&client->control_shm);
  661. client->control = 0;
  662. }
  663. if (req_fd >= 0) {
  664. close (req_fd);
  665. }
  666. if (ev_fd >= 0) {
  667. close (ev_fd);
  668. }
  669. return 0;
  670. }
  671. int
  672. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  673. {
  674. jack_client_connect_result_t res;
  675. int req_fd;
  676. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  677. }
  678. void
  679. jack_internal_client_close (const char *client_name)
  680. {
  681. jack_client_connect_request_t req;
  682. int fd;
  683. req.load = FALSE;
  684. snprintf (req.name, sizeof (req.name), "%s", client_name);
  685. if ((fd = server_connect (0)) < 0) {
  686. return;
  687. }
  688. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  689. jack_error ("cannot deliver ClientUnload request to JACK server.");
  690. }
  691. /* no response to this request */
  692. close (fd);
  693. return;
  694. }
  695. int
  696. jack_set_freewheel (jack_client_t* client, int onoff)
  697. {
  698. jack_request_t request;
  699. request.type = onoff ? FreeWheel : StopFreeWheel;
  700. return jack_client_deliver_request (client, &request);
  701. }
  702. void
  703. jack_start_freewheel (jack_client_t* client)
  704. {
  705. jack_client_control_t *control = client->control;
  706. if (client->engine->real_time) {
  707. #if JACK_USE_MACH_THREADS
  708. jack_drop_real_time_scheduling (client->process_thread);
  709. #else
  710. jack_drop_real_time_scheduling (client->thread);
  711. #endif
  712. }
  713. if (control->freewheel_cb) {
  714. control->freewheel_cb (1, control->freewheel_arg);
  715. }
  716. }
  717. void
  718. jack_stop_freewheel (jack_client_t* client)
  719. {
  720. jack_client_control_t *control = client->control;
  721. if (control->freewheel_cb) {
  722. control->freewheel_cb (0, control->freewheel_arg);
  723. }
  724. if (client->engine->real_time) {
  725. #if JACK_USE_MACH_THREADS
  726. jack_acquire_real_time_scheduling (client->process_thread,
  727. client->engine->client_priority);
  728. #else
  729. jack_acquire_real_time_scheduling (client->thread,
  730. client->engine->client_priority);
  731. #endif
  732. }
  733. }
  734. static void *
  735. jack_client_thread (void *arg)
  736. {
  737. jack_client_t *client = (jack_client_t *) arg;
  738. jack_client_control_t *control = client->control;
  739. jack_event_t event;
  740. char status = 0;
  741. char c = 0;
  742. int err = 0;
  743. pthread_mutex_lock (&client_lock);
  744. client->thread_ok = TRUE;
  745. client->thread_id = pthread_self();
  746. pthread_cond_signal (&client_ready);
  747. pthread_mutex_unlock (&client_lock);
  748. client->control->pid = getpid();
  749. client->control->pgrp = getpgrp();
  750. DEBUG ("client thread is now running");
  751. if (client->control->thread_init) {
  752. DEBUG ("calling client thread init callback");
  753. client->control->thread_init (client->control->thread_init_arg);
  754. }
  755. while (err == 0) {
  756. if (client->engine->engine_ok == 0) {
  757. if (client->on_shutdown)
  758. client->on_shutdown (client->on_shutdown_arg);
  759. else
  760. jack_error ("engine unexpectedly shutdown; "
  761. "thread exiting\n");
  762. pthread_exit (0);
  763. }
  764. DEBUG ("client polling on %s", client->pollmax == 2 ?
  765. "event_fd and graph_wait_fd..." :
  766. "event_fd only");
  767. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  768. if (errno == EINTR) {
  769. continue;
  770. }
  771. jack_error ("poll failed in client (%s)",
  772. strerror (errno));
  773. status = -1;
  774. break;
  775. }
  776. /* get an accurate timestamp on waking from poll for a
  777. * process() cycle.
  778. */
  779. if (client->graph_wait_fd >= 0 && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  780. control->awake_at = jack_get_microseconds();
  781. }
  782. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  783. client->pollfd[EVENT_POLL_INDEX].revents,
  784. client->pollfd[WAIT_POLL_INDEX].revents);
  785. pthread_testcancel();
  786. if (client->graph_wait_fd >= 0 &&
  787. (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. #ifdef JACK_USE_MACH_THREADS
  1071. /* Stephane Letz : letz@grame.fr
  1072. On MacOSX, the normal thread does not need to be real-time.
  1073. */
  1074. if (jack_create_thread (&client->thread,
  1075. client->engine->client_priority,
  1076. 0,
  1077. jack_client_thread, client)) {
  1078. return -1;
  1079. }
  1080. #else
  1081. if (jack_create_thread (&client->thread,
  1082. client->engine->client_priority,
  1083. client->engine->real_time,
  1084. jack_client_thread, client)) {
  1085. return -1;
  1086. }
  1087. #endif
  1088. #ifdef JACK_USE_MACH_THREADS
  1089. /* a secondary thread that runs the process callback and uses
  1090. ultra-fast Mach primitives for inter-thread signalling.
  1091. XXX in a properly structured JACK, there would be no
  1092. need for this, because we would have client wake up
  1093. methods that encapsulated the underlying mechanism
  1094. used.
  1095. */
  1096. if (jack_create_thread(&client->process_thread,
  1097. client->engine->client_priority,
  1098. client->engine->real_time,
  1099. jack_client_process_thread, client)) {
  1100. return -1;
  1101. }
  1102. #endif /* JACK_USE_MACH_THREADS */
  1103. return 0;
  1104. }
  1105. int
  1106. jack_activate (jack_client_t *client)
  1107. {
  1108. jack_request_t req;
  1109. /* we need to scribble on our stack to ensure that its memory
  1110. * pages are actually mapped (more important for mlockall(2)
  1111. * usage in jack_start_thread())
  1112. */
  1113. char buf[JACK_THREAD_STACK_TOUCH];
  1114. int i;
  1115. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1116. buf[i] = (char) (i & 0xff);
  1117. }
  1118. if (client->control->type == ClientInternal ||
  1119. client->control->type == ClientDriver) {
  1120. goto startit;
  1121. }
  1122. /* get the pid of the client process to pass it to engine */
  1123. client->control->pid = getpid ();
  1124. #ifdef USE_CAPABILITIES
  1125. if (client->engine->has_capabilities != 0 &&
  1126. client->control->pid != 0 && client->engine->real_time != 0) {
  1127. /* we need to ask the engine for realtime capabilities
  1128. before trying to start the realtime thread
  1129. */
  1130. req.type = SetClientCapabilities;
  1131. req.x.client_id = client->control->id;
  1132. jack_client_deliver_request (client, &req);
  1133. if (req.status) {
  1134. /* what to do? engine is running realtime, it
  1135. is using capabilities and has them
  1136. (otherwise we would not get an error
  1137. return) but for some reason it could not
  1138. give the client the required capabilities,
  1139. so for now downgrade the client so that it
  1140. still runs, albeit non-realtime - nando
  1141. */
  1142. jack_error ("could not receive realtime capabilities, "
  1143. "client will run non-realtime");
  1144. /* XXX wrong, this is a property of the engine
  1145. client->engine->real_time = 0;
  1146. */
  1147. }
  1148. }
  1149. #endif /* USE_CAPABILITIES */
  1150. if (client->first_active) {
  1151. pthread_mutex_init (&client_lock, NULL);
  1152. pthread_cond_init (&client_ready, NULL);
  1153. pthread_mutex_lock (&client_lock);
  1154. if (jack_start_thread (client)) {
  1155. pthread_mutex_unlock (&client_lock);
  1156. return -1;
  1157. }
  1158. pthread_cond_wait (&client_ready, &client_lock);
  1159. pthread_mutex_unlock (&client_lock);
  1160. if (!client->thread_ok) {
  1161. jack_error ("could not start client thread");
  1162. return -1;
  1163. }
  1164. client->first_active = FALSE;
  1165. }
  1166. startit:
  1167. req.type = ActivateClient;
  1168. req.x.client_id = client->control->id;
  1169. return jack_client_deliver_request (client, &req);
  1170. }
  1171. int
  1172. jack_deactivate (jack_client_t *client)
  1173. {
  1174. jack_request_t req;
  1175. req.type = DeactivateClient;
  1176. req.x.client_id = client->control->id;
  1177. return jack_client_deliver_request (client, &req);
  1178. }
  1179. int
  1180. jack_client_close (jack_client_t *client)
  1181. {
  1182. JSList *node;
  1183. void *status;
  1184. if (client->control->active) {
  1185. jack_deactivate (client);
  1186. }
  1187. if (client->control->type == ClientExternal) {
  1188. #if JACK_USE_MACH_THREADS
  1189. if (client->rt_thread_ok) {
  1190. // MacOSX pthread_cancel not implemented in
  1191. // Darwin 5.5, 6.4
  1192. mach_port_t machThread =
  1193. pthread_mach_thread_np (client->process_thread);
  1194. thread_terminate (machThread);
  1195. }
  1196. #endif
  1197. /* stop the thread that communicates with the jack
  1198. * server, only if it was actually running
  1199. */
  1200. if (client->thread_ok){
  1201. pthread_cancel (client->thread);
  1202. pthread_join (client->thread, &status);
  1203. }
  1204. if (client->control) {
  1205. jack_release_shm (&client->control_shm);
  1206. client->control = NULL;
  1207. }
  1208. if (client->engine) {
  1209. jack_release_shm (&client->engine_shm);
  1210. client->engine = NULL;
  1211. }
  1212. if (client->port_segment) {
  1213. jack_port_type_id_t ptid;
  1214. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1215. jack_release_shm (&client->port_segment[ptid]);
  1216. }
  1217. free (client->port_segment);
  1218. client->port_segment = NULL;
  1219. }
  1220. if (client->graph_wait_fd) {
  1221. close (client->graph_wait_fd);
  1222. }
  1223. if (client->graph_next_fd) {
  1224. close (client->graph_next_fd);
  1225. }
  1226. close (client->event_fd);
  1227. close (client->request_fd);
  1228. }
  1229. for (node = client->ports; node; node = jack_slist_next (node)) {
  1230. free (node->data);
  1231. }
  1232. jack_slist_free (client->ports);
  1233. jack_client_free (client);
  1234. return 0;
  1235. }
  1236. int
  1237. jack_is_realtime (jack_client_t *client)
  1238. {
  1239. return client->engine->real_time;
  1240. }
  1241. jack_nframes_t
  1242. jack_get_buffer_size (jack_client_t *client)
  1243. {
  1244. return client->engine->buffer_size;
  1245. }
  1246. int
  1247. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1248. {
  1249. #ifdef DO_BUFFER_RESIZE
  1250. jack_request_t req;
  1251. req.type = SetBufferSize;
  1252. req.x.nframes = nframes;
  1253. return jack_client_deliver_request (client, &req);
  1254. #else
  1255. return ENOSYS;
  1256. #endif /* DO_BUFFER_RESIZE */
  1257. }
  1258. int
  1259. jack_connect (jack_client_t *client, const char *source_port,
  1260. const char *destination_port)
  1261. {
  1262. jack_request_t req;
  1263. req.type = ConnectPorts;
  1264. snprintf (req.x.connect.source_port,
  1265. sizeof (req.x.connect.source_port), "%s", source_port);
  1266. snprintf (req.x.connect.destination_port,
  1267. sizeof (req.x.connect.destination_port),
  1268. "%s", destination_port);
  1269. return jack_client_deliver_request (client, &req);
  1270. }
  1271. int
  1272. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1273. {
  1274. jack_request_t req;
  1275. pthread_mutex_lock (&port->connection_lock);
  1276. if (port->connections == NULL) {
  1277. pthread_mutex_unlock (&port->connection_lock);
  1278. return 0;
  1279. }
  1280. pthread_mutex_unlock (&port->connection_lock);
  1281. req.type = DisconnectPort;
  1282. req.x.port_info.port_id = port->shared->id;
  1283. return jack_client_deliver_request (client, &req);
  1284. }
  1285. int
  1286. jack_disconnect (jack_client_t *client, const char *source_port,
  1287. const char *destination_port)
  1288. {
  1289. jack_request_t req;
  1290. req.type = DisconnectPorts;
  1291. snprintf (req.x.connect.source_port,
  1292. sizeof (req.x.connect.source_port), "%s", source_port);
  1293. snprintf (req.x.connect.destination_port,
  1294. sizeof (req.x.connect.destination_port),
  1295. "%s", destination_port);
  1296. return jack_client_deliver_request (client, &req);
  1297. }
  1298. void
  1299. jack_set_error_function (void (*func) (const char *))
  1300. {
  1301. jack_error_callback = func;
  1302. }
  1303. int
  1304. jack_set_graph_order_callback (jack_client_t *client,
  1305. JackGraphOrderCallback callback, void *arg)
  1306. {
  1307. if (client->control->active) {
  1308. jack_error ("You cannot set callbacks on an active client.");
  1309. return -1;
  1310. }
  1311. client->control->graph_order = callback;
  1312. client->control->graph_order_arg = arg;
  1313. return 0;
  1314. }
  1315. int jack_set_xrun_callback (jack_client_t *client,
  1316. JackXRunCallback 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->xrun = callback;
  1323. client->control->xrun_arg = arg;
  1324. return 0;
  1325. }
  1326. int
  1327. jack_set_process_callback (jack_client_t *client,
  1328. JackProcessCallback 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->process_arg = arg;
  1335. client->control->process = callback;
  1336. return 0;
  1337. }
  1338. int
  1339. jack_set_thread_init_callback (jack_client_t *client,
  1340. JackThreadInitCallback 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->thread_init_arg = arg;
  1347. client->control->thread_init = callback;
  1348. return 0;
  1349. }
  1350. int
  1351. jack_set_freewheel_callback (jack_client_t *client,
  1352. JackFreewheelCallback callback, void *arg)
  1353. {
  1354. if (client->control->active) {
  1355. jack_error ("You cannot set callbacks on an active client.");
  1356. return -1;
  1357. }
  1358. client->control->freewheel_arg = arg;
  1359. client->control->freewheel_cb = callback;
  1360. return 0;
  1361. }
  1362. int
  1363. jack_set_buffer_size_callback (jack_client_t *client,
  1364. JackBufferSizeCallback callback, void *arg)
  1365. {
  1366. client->control->bufsize_arg = arg;
  1367. client->control->bufsize = callback;
  1368. return 0;
  1369. }
  1370. int
  1371. jack_set_port_registration_callback(jack_client_t *client,
  1372. JackPortRegistrationCallback callback,
  1373. void *arg)
  1374. {
  1375. if (client->control->active) {
  1376. jack_error ("You cannot set callbacks on an active client.");
  1377. return -1;
  1378. }
  1379. client->control->port_register_arg = arg;
  1380. client->control->port_register = callback;
  1381. return 0;
  1382. }
  1383. int
  1384. jack_get_process_done_fd (jack_client_t *client)
  1385. {
  1386. return client->graph_next_fd;
  1387. }
  1388. void
  1389. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1390. {
  1391. client->on_shutdown = function;
  1392. client->on_shutdown_arg = arg;
  1393. }
  1394. const char **
  1395. jack_get_ports (jack_client_t *client,
  1396. const char *port_name_pattern,
  1397. const char *type_name_pattern,
  1398. unsigned long flags)
  1399. {
  1400. jack_control_t *engine;
  1401. const char **matching_ports;
  1402. unsigned long match_cnt;
  1403. jack_port_shared_t *psp;
  1404. unsigned long i;
  1405. regex_t port_regex;
  1406. regex_t type_regex;
  1407. int matching;
  1408. engine = client->engine;
  1409. if (port_name_pattern && port_name_pattern[0]) {
  1410. regcomp (&port_regex, port_name_pattern,
  1411. REG_EXTENDED|REG_NOSUB);
  1412. }
  1413. if (type_name_pattern && type_name_pattern[0]) {
  1414. regcomp (&type_regex, type_name_pattern,
  1415. REG_EXTENDED|REG_NOSUB);
  1416. }
  1417. psp = engine->ports;
  1418. match_cnt = 0;
  1419. matching_ports = (const char **)
  1420. malloc (sizeof (char *) * engine->port_max);
  1421. for (i = 0; i < engine->port_max; i++) {
  1422. matching = 1;
  1423. if (!psp[i].in_use) {
  1424. continue;
  1425. }
  1426. if (flags) {
  1427. if ((psp[i].flags & flags) != flags) {
  1428. matching = 0;
  1429. }
  1430. }
  1431. if (matching && port_name_pattern && port_name_pattern[0]) {
  1432. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1433. matching = 0;
  1434. }
  1435. }
  1436. if (matching && type_name_pattern && type_name_pattern[0]) {
  1437. jack_port_type_id_t ptid = psp[i].ptype_id;
  1438. if (regexec (&type_regex,
  1439. engine->port_types[ptid].type_name,
  1440. 0, NULL, 0)) {
  1441. matching = 0;
  1442. }
  1443. }
  1444. if (matching) {
  1445. matching_ports[match_cnt++] = psp[i].name;
  1446. }
  1447. }
  1448. if (port_name_pattern && port_name_pattern[0]) {
  1449. regfree (&port_regex);
  1450. }
  1451. if (type_name_pattern && type_name_pattern[0]) {
  1452. regfree (&type_regex);
  1453. }
  1454. matching_ports[match_cnt] = 0;
  1455. if (match_cnt == 0) {
  1456. free (matching_ports);
  1457. matching_ports = 0;
  1458. }
  1459. return matching_ports;
  1460. }
  1461. float
  1462. jack_cpu_load (jack_client_t *client)
  1463. {
  1464. return client->engine->cpu_load;
  1465. }
  1466. pthread_t
  1467. jack_client_thread_id (jack_client_t *client)
  1468. {
  1469. return client->thread_id;
  1470. }
  1471. int
  1472. jack_client_name_size(void)
  1473. {
  1474. return JACK_CLIENT_NAME_SIZE;
  1475. }
  1476. int
  1477. jack_port_name_size(void)
  1478. {
  1479. return JACK_PORT_NAME_SIZE;
  1480. }
  1481. int
  1482. jack_port_type_size(void)
  1483. {
  1484. return JACK_PORT_TYPE_SIZE;
  1485. }