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.

1979 lines
46KB

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