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.

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