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.

1972 lines
45KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #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_%d",
  276. jack_server_dir (server_name), getuid (), 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_%d_ack_0",
  297. jack_server_dir (server_name), getuid () );
  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_initialize_shm (va.server_name)) {
  625. jack_error ("Unable to initialize shared memory.");
  626. return NULL;
  627. }
  628. if (jack_request_client (ClientExternal, client_name, options, status,
  629. &va, &res, &req_fd)) {
  630. return NULL;
  631. }
  632. client = jack_client_alloc ();
  633. strcpy (client->name, res.name);
  634. strcpy (client->fifo_prefix, res.fifo_prefix);
  635. client->request_fd = req_fd;
  636. client->pollfd[EVENT_POLL_INDEX].events =
  637. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  638. client->pollfd[WAIT_POLL_INDEX].events =
  639. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  640. /* attach the engine control/info block */
  641. client->engine_shm = res.engine_shm;
  642. if (jack_attach_shm (&client->engine_shm)) {
  643. jack_error ("cannot attached engine control shared memory"
  644. " segment");
  645. goto fail;
  646. }
  647. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  648. /* now attach the client control block */
  649. client->control_shm = res.client_shm;
  650. if (jack_attach_shm (&client->control_shm)) {
  651. jack_error ("cannot attached client control shared memory"
  652. " segment");
  653. goto fail;
  654. }
  655. client->control = (jack_client_control_t *)
  656. jack_shm_addr (&client->control_shm);
  657. /* nobody else needs to access this shared memory any more, so
  658. destroy it. because we have our own attachment to it, it won't
  659. vanish till we exit (and release it).
  660. */
  661. jack_destroy_shm (&client->control_shm);
  662. client->n_port_types = client->engine->n_port_types;
  663. client->port_segment = (jack_shm_info_t *)
  664. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  665. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  666. client->port_segment[ptid].index =
  667. client->engine->port_types[ptid].shm_registry_index;
  668. client->port_segment[ptid].attached_at = MAP_FAILED;
  669. jack_attach_port_segment (client, ptid);
  670. }
  671. /* set up the client so that it does the right thing for an
  672. * external client
  673. */
  674. client->control->deliver_request = oop_client_deliver_request;
  675. client->control->deliver_arg = client;
  676. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  677. goto fail;
  678. }
  679. client->event_fd = ev_fd;
  680. #ifdef JACK_USE_MACH_THREADS
  681. /* specific resources for server/client real-time thread
  682. * communication */
  683. client->clienttask = mach_task_self();
  684. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  685. jack_error ("Can't find bootstrap port");
  686. goto fail;
  687. }
  688. if (allocate_mach_clientport(client, res.portnum) < 0) {
  689. jack_error("Can't allocate mach port");
  690. goto fail;
  691. };
  692. #endif /* JACK_USE_MACH_THREADS */
  693. return client;
  694. fail:
  695. if (client->engine) {
  696. jack_release_shm (&client->engine_shm);
  697. client->engine = 0;
  698. }
  699. if (client->control) {
  700. jack_release_shm (&client->control_shm);
  701. client->control = 0;
  702. }
  703. if (req_fd >= 0) {
  704. close (req_fd);
  705. }
  706. if (ev_fd >= 0) {
  707. close (ev_fd);
  708. }
  709. return 0;
  710. }
  711. jack_client_t *
  712. jack_client_new (const char *client_name)
  713. {
  714. jack_options_t options = JackUseExactName;
  715. if (getenv("JACK_START_SERVER") == NULL)
  716. options |= JackNoStartServer;
  717. return jack_client_open (client_name, options, NULL);
  718. }
  719. char *
  720. jack_get_client_name (jack_client_t *client)
  721. {
  722. return client->name;
  723. }
  724. int
  725. jack_internal_client_new (const char *client_name,
  726. const char *so_name, const char *so_data)
  727. {
  728. jack_client_connect_result_t res;
  729. int req_fd;
  730. jack_varargs_t va;
  731. jack_status_t status;
  732. jack_options_t options = JackUseExactName;
  733. if (getenv("JACK_START_SERVER") == NULL)
  734. options |= JackNoStartServer;
  735. jack_varargs_init (&va);
  736. va.load_name = (char *) so_name;
  737. va.load_init = (char *) so_data;
  738. return jack_request_client (ClientInternal, client_name,
  739. options, &status, &va, &res, &req_fd);
  740. }
  741. char *
  742. jack_default_server_name (void)
  743. {
  744. char *server_name;
  745. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  746. server_name = "default";
  747. return server_name;
  748. }
  749. char *jack_tmpdir = DEFAULT_TMP_DIR;
  750. /* returns the name of the per-user subdirectory of jack_tmpdir */
  751. char *
  752. jack_user_dir (void)
  753. {
  754. static char user_dir[PATH_MAX] = "";
  755. /* format the path name on the first call */
  756. if (user_dir[0] == '\0') {
  757. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  758. jack_tmpdir, getuid ());
  759. }
  760. return user_dir;
  761. }
  762. /* returns the name of the per-server subdirectory of jack_user_dir() */
  763. char *
  764. jack_server_dir (const char *server_name)
  765. {
  766. static char server_dir[PATH_MAX] = "";
  767. /* format the path name on the first call */
  768. if (server_dir[0] == '\0') {
  769. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  770. jack_user_dir (), server_name);
  771. }
  772. return server_dir;
  773. }
  774. void
  775. jack_internal_client_close (const char *client_name)
  776. {
  777. jack_client_connect_request_t req;
  778. int fd;
  779. char *server_name = jack_default_server_name ();
  780. req.load = FALSE;
  781. snprintf (req.name, sizeof (req.name), "%s", client_name);
  782. if ((fd = server_connect (server_name)) < 0) {
  783. return;
  784. }
  785. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  786. jack_error ("cannot deliver ClientUnload request to JACK "
  787. "server.");
  788. }
  789. /* no response to this request */
  790. close (fd);
  791. return;
  792. }
  793. int
  794. jack_set_freewheel (jack_client_t* client, int onoff)
  795. {
  796. jack_request_t request;
  797. request.type = onoff ? FreeWheel : StopFreeWheel;
  798. return jack_client_deliver_request (client, &request);
  799. }
  800. void
  801. jack_start_freewheel (jack_client_t* client)
  802. {
  803. jack_client_control_t *control = client->control;
  804. if (client->engine->real_time) {
  805. #if JACK_USE_MACH_THREADS
  806. jack_drop_real_time_scheduling (client->process_thread);
  807. #else
  808. jack_drop_real_time_scheduling (client->thread);
  809. #endif
  810. }
  811. if (control->freewheel_cb) {
  812. control->freewheel_cb (1, control->freewheel_arg);
  813. }
  814. }
  815. void
  816. jack_stop_freewheel (jack_client_t* client)
  817. {
  818. jack_client_control_t *control = client->control;
  819. if (control->freewheel_cb) {
  820. control->freewheel_cb (0, control->freewheel_arg);
  821. }
  822. if (client->engine->real_time) {
  823. #if JACK_USE_MACH_THREADS
  824. jack_acquire_real_time_scheduling (client->process_thread,
  825. client->engine->client_priority);
  826. #else
  827. jack_acquire_real_time_scheduling (client->thread,
  828. client->engine->client_priority);
  829. #endif
  830. }
  831. }
  832. static void *
  833. jack_client_thread (void *arg)
  834. {
  835. jack_client_t *client = (jack_client_t *) arg;
  836. jack_client_control_t *control = client->control;
  837. jack_event_t event;
  838. char status = 0;
  839. char c = 0;
  840. int err = 0;
  841. pthread_mutex_lock (&client_lock);
  842. client->thread_ok = TRUE;
  843. client->thread_id = pthread_self();
  844. pthread_cond_signal (&client_ready);
  845. pthread_mutex_unlock (&client_lock);
  846. client->control->pid = getpid();
  847. client->control->pgrp = getpgrp();
  848. DEBUG ("client thread is now running");
  849. if (client->control->thread_init) {
  850. DEBUG ("calling client thread init callback");
  851. client->control->thread_init (client->control->thread_init_arg);
  852. }
  853. while (err == 0) {
  854. if (client->engine->engine_ok == 0) {
  855. if (client->on_shutdown)
  856. client->on_shutdown (client->on_shutdown_arg);
  857. else
  858. jack_error ("engine unexpectedly shutdown; "
  859. "thread exiting\n");
  860. pthread_exit (0);
  861. }
  862. DEBUG ("client polling on %s", client->pollmax == 2 ?
  863. "event_fd and graph_wait_fd..." :
  864. "event_fd only");
  865. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  866. if (errno == EINTR) {
  867. continue;
  868. }
  869. jack_error ("poll failed in client (%s)",
  870. strerror (errno));
  871. status = -1;
  872. break;
  873. }
  874. /* get an accurate timestamp on waking from poll for a
  875. * process() cycle.
  876. */
  877. if (client->graph_wait_fd >= 0 && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  878. control->awake_at = jack_get_microseconds();
  879. }
  880. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  881. client->pollfd[EVENT_POLL_INDEX].revents,
  882. client->pollfd[WAIT_POLL_INDEX].revents);
  883. pthread_testcancel();
  884. if (client->graph_wait_fd >= 0 &&
  885. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  886. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR, ZOMBIE\n\n\n\n\n");
  887. /* our upstream "wait" connection
  888. closed, which either means that
  889. an intermediate client exited, or
  890. jackd exited, or jackd zombified
  891. us.
  892. we can discover the zombification
  893. via client->control->dead, but
  894. the other two possibilities are
  895. impossible to identify just from
  896. this situation. so we have to
  897. check what we are connected to,
  898. and act accordingly.
  899. */
  900. if (client->upstream_is_jackd) {
  901. DEBUG ("WE DIE\n");
  902. goto zombie;
  903. } else {
  904. DEBUG ("WE PUNT\n");
  905. /* don't poll on the wait fd
  906. * again until we get a
  907. * GraphReordered event.
  908. */
  909. client->graph_wait_fd = -1;
  910. client->pollmax = 1;
  911. }
  912. }
  913. if (client->control->dead) {
  914. goto zombie;
  915. }
  916. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  917. /* jackd shutdown */
  918. goto zombie;
  919. }
  920. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  921. DEBUG ("client receives an event, "
  922. "now reading on event fd");
  923. /* server has sent us an event. process the
  924. * event and reply */
  925. if (read (client->event_fd, &event, sizeof (event))
  926. != sizeof (event)) {
  927. jack_error ("cannot read server event (%s)",
  928. strerror (errno));
  929. err++;
  930. break;
  931. }
  932. status = 0;
  933. switch (event.type) {
  934. case PortRegistered:
  935. if (control->port_register) {
  936. control->port_register
  937. (event.x.port_id, TRUE,
  938. control->port_register_arg);
  939. }
  940. break;
  941. case PortUnregistered:
  942. if (control->port_register) {
  943. control->port_register
  944. (event.x.port_id, FALSE,
  945. control->port_register_arg);
  946. }
  947. break;
  948. case GraphReordered:
  949. status = jack_handle_reorder (client, &event);
  950. break;
  951. case PortConnected:
  952. case PortDisconnected:
  953. status = jack_client_handle_port_connection
  954. (client, &event);
  955. break;
  956. case BufferSizeChange:
  957. jack_client_invalidate_port_buffers (client);
  958. if (control->bufsize) {
  959. status = control->bufsize
  960. (control->nframes,
  961. control->bufsize_arg);
  962. }
  963. break;
  964. case SampleRateChange:
  965. if (control->srate) {
  966. status = control->srate
  967. (control->nframes,
  968. control->srate_arg);
  969. }
  970. break;
  971. case XRun:
  972. if (control->xrun) {
  973. status = control->xrun
  974. (control->xrun_arg);
  975. }
  976. break;
  977. case AttachPortSegment:
  978. jack_attach_port_segment (client, event.y.ptid);
  979. break;
  980. case StartFreewheel:
  981. jack_start_freewheel (client);
  982. break;
  983. case StopFreewheel:
  984. jack_stop_freewheel (client);
  985. break;
  986. }
  987. DEBUG ("client has dealt with the event, writing "
  988. "response on event fd");
  989. if (write (client->event_fd, &status, sizeof (status))
  990. != sizeof (status)) {
  991. jack_error ("cannot send event response to "
  992. "engine (%s)", strerror (errno));
  993. err++;
  994. break;
  995. }
  996. }
  997. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  998. #ifdef WITH_TIMESTAMPS
  999. jack_reset_timestamps ();
  1000. #endif
  1001. DEBUG ("client %d signalled at %" PRIu64
  1002. ", awake for process at %" PRIu64
  1003. " (delay = %" PRIu64
  1004. " usecs) (wakeup on graph_wait_fd==%d)",
  1005. getpid(),
  1006. control->signalled_at,
  1007. control->awake_at,
  1008. control->awake_at - control->signalled_at,
  1009. client->pollfd[WAIT_POLL_INDEX].fd);
  1010. control->state = Running;
  1011. if (control->sync_cb)
  1012. jack_call_sync_client (client);
  1013. if (control->process) {
  1014. if (control->process (control->nframes,
  1015. control->process_arg)
  1016. == 0) {
  1017. control->state = Finished;
  1018. }
  1019. } else {
  1020. control->state = Finished;
  1021. }
  1022. if (control->timebase_cb)
  1023. jack_call_timebase_master (client);
  1024. control->finished_at = jack_get_microseconds();
  1025. #ifdef WITH_TIMESTAMPS
  1026. jack_timestamp ("finished");
  1027. #endif
  1028. /* pass the execution token along */
  1029. DEBUG ("client finished processing at %" PRIu64
  1030. " (elapsed = %" PRIu64
  1031. " usecs), writing on graph_next_fd==%d",
  1032. control->finished_at,
  1033. control->finished_at - control->awake_at,
  1034. client->graph_next_fd);
  1035. if (write (client->graph_next_fd, &c, sizeof (c))
  1036. != sizeof (c)) {
  1037. jack_error ("cannot continue execution of the "
  1038. "processing graph (%s)",
  1039. strerror(errno));
  1040. err++;
  1041. break;
  1042. }
  1043. DEBUG ("client sent message to next stage by %" PRIu64
  1044. ", client reading on graph_wait_fd==%d",
  1045. jack_get_microseconds(), client->graph_wait_fd);
  1046. #ifdef WITH_TIMESTAMPS
  1047. jack_timestamp ("read pending byte from wait");
  1048. #endif
  1049. DEBUG("reading cleanup byte from pipe\n");
  1050. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1051. != sizeof (c))) {
  1052. DEBUG ("WARNING: READ FAILED!");
  1053. #if 0
  1054. jack_error ("cannot complete execution of the "
  1055. "processing graph (%s)",
  1056. strerror(errno));
  1057. err++;
  1058. break;
  1059. #endif
  1060. }
  1061. /* check if we were killed during the process
  1062. * cycle (or whatever) */
  1063. if (client->control->dead) {
  1064. goto zombie;
  1065. }
  1066. DEBUG("process cycle fully complete\n");
  1067. #ifdef WITH_TIMESTAMPS
  1068. jack_timestamp ("read done");
  1069. jack_dump_timestamps (stdout);
  1070. #endif
  1071. }
  1072. }
  1073. return (void *) ((intptr_t)err);
  1074. zombie:
  1075. if (client->on_shutdown) {
  1076. jack_error ("zombified - calling shutdown handler");
  1077. client->on_shutdown (client->on_shutdown_arg);
  1078. } else {
  1079. jack_error ("zombified - exiting from JACK");
  1080. jack_client_close (client);
  1081. /* Need a fix : possibly make client crash if
  1082. * zombified without shutdown handler
  1083. */
  1084. }
  1085. pthread_exit (0);
  1086. /*NOTREACHED*/
  1087. return 0;
  1088. }
  1089. #ifdef JACK_USE_MACH_THREADS
  1090. /* real-time thread : separated from the normal client thread, it will
  1091. * communicate with the server using fast mach RPC mechanism */
  1092. static void *
  1093. jack_client_process_thread (void *arg)
  1094. {
  1095. jack_client_t *client = (jack_client_t *) arg;
  1096. jack_client_control_t *control = client->control;
  1097. int err = 0;
  1098. if (client->control->thread_init) {
  1099. /* this means that the init callback will be called twice -taybin*/
  1100. DEBUG ("calling client thread init callback");
  1101. client->control->thread_init (client->control->thread_init_arg);
  1102. }
  1103. client->control->pid = getpid();
  1104. DEBUG ("client process thread is now running");
  1105. client->rt_thread_ok = TRUE;
  1106. while (err == 0) {
  1107. if (jack_client_suspend(client) < 0) {
  1108. jack_error ("jack_client_process_thread : resume error");
  1109. goto zombie;
  1110. }
  1111. control->awake_at = jack_get_microseconds();
  1112. DEBUG ("client resumed");
  1113. control->state = Running;
  1114. if (control->sync_cb)
  1115. jack_call_sync_client (client);
  1116. if (control->process) {
  1117. if (control->process (control->nframes,
  1118. control->process_arg) == 0) {
  1119. control->state = Finished;
  1120. }
  1121. } else {
  1122. control->state = Finished;
  1123. }
  1124. if (control->timebase_cb)
  1125. jack_call_timebase_master (client);
  1126. control->finished_at = jack_get_microseconds();
  1127. #ifdef WITH_TIMESTAMPS
  1128. jack_timestamp ("finished");
  1129. #endif
  1130. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1131. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  1132. /* check if we were killed during the process cycle (or whatever) */
  1133. if (client->control->dead) {
  1134. jack_error ("jack_client_process_thread : client->control->dead");
  1135. goto zombie;
  1136. }
  1137. DEBUG("process cycle fully complete\n");
  1138. }
  1139. return (void *) ((intptr_t)err);
  1140. zombie:
  1141. jack_error ("jack_client_process_thread : zombified");
  1142. client->rt_thread_ok = FALSE;
  1143. if (client->on_shutdown) {
  1144. jack_error ("zombified - calling shutdown handler");
  1145. client->on_shutdown (client->on_shutdown_arg);
  1146. } else {
  1147. jack_error ("zombified - exiting from JACK");
  1148. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  1149. }
  1150. pthread_exit (0);
  1151. /*NOTREACHED*/
  1152. return 0;
  1153. }
  1154. #endif /* JACK_USE_MACH_THREADS */
  1155. static int
  1156. jack_start_thread (jack_client_t *client)
  1157. {
  1158. if (client->engine->real_time) {
  1159. #ifdef USE_MLOCK
  1160. if (client->engine->do_mlock
  1161. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1162. jack_error ("cannot lock down memory for RT thread (%s)",
  1163. strerror (errno));
  1164. #ifdef ENSURE_MLOCK
  1165. return -1;
  1166. #endif /* ENSURE_MLOCK */
  1167. }
  1168. if (client->engine->do_munlock) {
  1169. cleanup_mlock ();
  1170. }
  1171. #endif /* USE_MLOCK */
  1172. }
  1173. #ifdef JACK_USE_MACH_THREADS
  1174. /* Stephane Letz : letz@grame.fr
  1175. On MacOSX, the normal thread does not need to be real-time.
  1176. */
  1177. if (jack_create_thread (&client->thread,
  1178. client->engine->client_priority,
  1179. 0,
  1180. jack_client_thread, client)) {
  1181. return -1;
  1182. }
  1183. #else
  1184. if (jack_create_thread (&client->thread,
  1185. client->engine->client_priority,
  1186. client->engine->real_time,
  1187. jack_client_thread, client)) {
  1188. return -1;
  1189. }
  1190. #endif
  1191. #ifdef JACK_USE_MACH_THREADS
  1192. /* a secondary thread that runs the process callback and uses
  1193. ultra-fast Mach primitives for inter-thread signalling.
  1194. XXX in a properly structured JACK, there would be no
  1195. need for this, because we would have client wake up
  1196. methods that encapsulated the underlying mechanism
  1197. used.
  1198. */
  1199. if (jack_create_thread(&client->process_thread,
  1200. client->engine->client_priority,
  1201. client->engine->real_time,
  1202. jack_client_process_thread, client)) {
  1203. return -1;
  1204. }
  1205. #endif /* JACK_USE_MACH_THREADS */
  1206. return 0;
  1207. }
  1208. int
  1209. jack_activate (jack_client_t *client)
  1210. {
  1211. jack_request_t req;
  1212. /* we need to scribble on our stack to ensure that its memory
  1213. * pages are actually mapped (more important for mlockall(2)
  1214. * usage in jack_start_thread())
  1215. */
  1216. char buf[JACK_THREAD_STACK_TOUCH];
  1217. int i;
  1218. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1219. buf[i] = (char) (i & 0xff);
  1220. }
  1221. if (client->control->type == ClientInternal ||
  1222. client->control->type == ClientDriver) {
  1223. goto startit;
  1224. }
  1225. /* get the pid of the client process to pass it to engine */
  1226. client->control->pid = getpid ();
  1227. #ifdef USE_CAPABILITIES
  1228. if (client->engine->has_capabilities != 0 &&
  1229. client->control->pid != 0 && client->engine->real_time != 0) {
  1230. /* we need to ask the engine for realtime capabilities
  1231. before trying to start the realtime thread
  1232. */
  1233. req.type = SetClientCapabilities;
  1234. req.x.client_id = client->control->id;
  1235. jack_client_deliver_request (client, &req);
  1236. if (req.status) {
  1237. /* what to do? engine is running realtime, it
  1238. is using capabilities and has them
  1239. (otherwise we would not get an error
  1240. return) but for some reason it could not
  1241. give the client the required capabilities,
  1242. so for now downgrade the client so that it
  1243. still runs, albeit non-realtime - nando
  1244. */
  1245. jack_error ("could not receive realtime capabilities, "
  1246. "client will run non-realtime");
  1247. /* XXX wrong, this is a property of the engine
  1248. client->engine->real_time = 0;
  1249. */
  1250. }
  1251. }
  1252. #endif /* USE_CAPABILITIES */
  1253. if (client->first_active) {
  1254. pthread_mutex_init (&client_lock, NULL);
  1255. pthread_cond_init (&client_ready, NULL);
  1256. pthread_mutex_lock (&client_lock);
  1257. if (jack_start_thread (client)) {
  1258. pthread_mutex_unlock (&client_lock);
  1259. return -1;
  1260. }
  1261. pthread_cond_wait (&client_ready, &client_lock);
  1262. pthread_mutex_unlock (&client_lock);
  1263. if (!client->thread_ok) {
  1264. jack_error ("could not start client thread");
  1265. return -1;
  1266. }
  1267. client->first_active = FALSE;
  1268. }
  1269. startit:
  1270. req.type = ActivateClient;
  1271. req.x.client_id = client->control->id;
  1272. return jack_client_deliver_request (client, &req);
  1273. }
  1274. int
  1275. jack_deactivate (jack_client_t *client)
  1276. {
  1277. jack_request_t req;
  1278. req.type = DeactivateClient;
  1279. req.x.client_id = client->control->id;
  1280. return jack_client_deliver_request (client, &req);
  1281. }
  1282. int
  1283. jack_client_close (jack_client_t *client)
  1284. {
  1285. JSList *node;
  1286. void *status;
  1287. if (client->control->active) {
  1288. jack_deactivate (client);
  1289. }
  1290. if (client->control->type == ClientExternal) {
  1291. #if JACK_USE_MACH_THREADS
  1292. if (client->rt_thread_ok) {
  1293. // MacOSX pthread_cancel not implemented in
  1294. // Darwin 5.5, 6.4
  1295. mach_port_t machThread =
  1296. pthread_mach_thread_np (client->process_thread);
  1297. thread_terminate (machThread);
  1298. }
  1299. #endif
  1300. /* stop the thread that communicates with the jack
  1301. * server, only if it was actually running
  1302. */
  1303. if (client->thread_ok){
  1304. pthread_cancel (client->thread);
  1305. pthread_join (client->thread, &status);
  1306. }
  1307. if (client->control) {
  1308. jack_release_shm (&client->control_shm);
  1309. client->control = NULL;
  1310. }
  1311. if (client->engine) {
  1312. jack_release_shm (&client->engine_shm);
  1313. client->engine = NULL;
  1314. }
  1315. if (client->port_segment) {
  1316. jack_port_type_id_t ptid;
  1317. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1318. jack_release_shm (&client->port_segment[ptid]);
  1319. }
  1320. free (client->port_segment);
  1321. client->port_segment = NULL;
  1322. }
  1323. if (client->graph_wait_fd) {
  1324. close (client->graph_wait_fd);
  1325. }
  1326. if (client->graph_next_fd) {
  1327. close (client->graph_next_fd);
  1328. }
  1329. close (client->event_fd);
  1330. close (client->request_fd);
  1331. }
  1332. for (node = client->ports; node; node = jack_slist_next (node)) {
  1333. free (node->data);
  1334. }
  1335. jack_slist_free (client->ports);
  1336. jack_client_free (client);
  1337. return 0;
  1338. }
  1339. int
  1340. jack_is_realtime (jack_client_t *client)
  1341. {
  1342. return client->engine->real_time;
  1343. }
  1344. jack_nframes_t
  1345. jack_get_buffer_size (jack_client_t *client)
  1346. {
  1347. return client->engine->buffer_size;
  1348. }
  1349. int
  1350. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1351. {
  1352. #ifdef DO_BUFFER_RESIZE
  1353. jack_request_t req;
  1354. req.type = SetBufferSize;
  1355. req.x.nframes = nframes;
  1356. return jack_client_deliver_request (client, &req);
  1357. #else
  1358. return ENOSYS;
  1359. #endif /* DO_BUFFER_RESIZE */
  1360. }
  1361. int
  1362. jack_connect (jack_client_t *client, const char *source_port,
  1363. const char *destination_port)
  1364. {
  1365. jack_request_t req;
  1366. req.type = ConnectPorts;
  1367. snprintf (req.x.connect.source_port,
  1368. sizeof (req.x.connect.source_port), "%s", source_port);
  1369. snprintf (req.x.connect.destination_port,
  1370. sizeof (req.x.connect.destination_port),
  1371. "%s", destination_port);
  1372. return jack_client_deliver_request (client, &req);
  1373. }
  1374. int
  1375. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1376. {
  1377. jack_request_t req;
  1378. pthread_mutex_lock (&port->connection_lock);
  1379. if (port->connections == NULL) {
  1380. pthread_mutex_unlock (&port->connection_lock);
  1381. return 0;
  1382. }
  1383. pthread_mutex_unlock (&port->connection_lock);
  1384. req.type = DisconnectPort;
  1385. req.x.port_info.port_id = port->shared->id;
  1386. return jack_client_deliver_request (client, &req);
  1387. }
  1388. int
  1389. jack_disconnect (jack_client_t *client, const char *source_port,
  1390. const char *destination_port)
  1391. {
  1392. jack_request_t req;
  1393. req.type = DisconnectPorts;
  1394. snprintf (req.x.connect.source_port,
  1395. sizeof (req.x.connect.source_port), "%s", source_port);
  1396. snprintf (req.x.connect.destination_port,
  1397. sizeof (req.x.connect.destination_port),
  1398. "%s", destination_port);
  1399. return jack_client_deliver_request (client, &req);
  1400. }
  1401. void
  1402. jack_set_error_function (void (*func) (const char *))
  1403. {
  1404. jack_error_callback = func;
  1405. }
  1406. int
  1407. jack_set_graph_order_callback (jack_client_t *client,
  1408. JackGraphOrderCallback callback, void *arg)
  1409. {
  1410. if (client->control->active) {
  1411. jack_error ("You cannot set callbacks on an active client.");
  1412. return -1;
  1413. }
  1414. client->control->graph_order = callback;
  1415. client->control->graph_order_arg = arg;
  1416. return 0;
  1417. }
  1418. int jack_set_xrun_callback (jack_client_t *client,
  1419. JackXRunCallback callback, void *arg)
  1420. {
  1421. if (client->control->active) {
  1422. jack_error ("You cannot set callbacks on an active client.");
  1423. return -1;
  1424. }
  1425. client->control->xrun = callback;
  1426. client->control->xrun_arg = arg;
  1427. return 0;
  1428. }
  1429. int
  1430. jack_set_process_callback (jack_client_t *client,
  1431. JackProcessCallback callback, void *arg)
  1432. {
  1433. if (client->control->active) {
  1434. jack_error ("You cannot set callbacks on an active client.");
  1435. return -1;
  1436. }
  1437. client->control->process_arg = arg;
  1438. client->control->process = callback;
  1439. return 0;
  1440. }
  1441. int
  1442. jack_set_thread_init_callback (jack_client_t *client,
  1443. JackThreadInitCallback callback, void *arg)
  1444. {
  1445. if (client->control->active) {
  1446. jack_error ("You cannot set callbacks on an active client.");
  1447. return -1;
  1448. }
  1449. client->control->thread_init_arg = arg;
  1450. client->control->thread_init = callback;
  1451. return 0;
  1452. }
  1453. int
  1454. jack_set_freewheel_callback (jack_client_t *client,
  1455. JackFreewheelCallback callback, void *arg)
  1456. {
  1457. if (client->control->active) {
  1458. jack_error ("You cannot set callbacks on an active client.");
  1459. return -1;
  1460. }
  1461. client->control->freewheel_arg = arg;
  1462. client->control->freewheel_cb = callback;
  1463. return 0;
  1464. }
  1465. int
  1466. jack_set_buffer_size_callback (jack_client_t *client,
  1467. JackBufferSizeCallback callback, void *arg)
  1468. {
  1469. client->control->bufsize_arg = arg;
  1470. client->control->bufsize = callback;
  1471. return 0;
  1472. }
  1473. int
  1474. jack_set_port_registration_callback(jack_client_t *client,
  1475. JackPortRegistrationCallback callback,
  1476. 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->port_register_arg = arg;
  1483. client->control->port_register = callback;
  1484. return 0;
  1485. }
  1486. int
  1487. jack_get_process_done_fd (jack_client_t *client)
  1488. {
  1489. return client->graph_next_fd;
  1490. }
  1491. void
  1492. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1493. {
  1494. client->on_shutdown = function;
  1495. client->on_shutdown_arg = arg;
  1496. }
  1497. const char **
  1498. jack_get_ports (jack_client_t *client,
  1499. const char *port_name_pattern,
  1500. const char *type_name_pattern,
  1501. unsigned long flags)
  1502. {
  1503. jack_control_t *engine;
  1504. const char **matching_ports;
  1505. unsigned long match_cnt;
  1506. jack_port_shared_t *psp;
  1507. unsigned long i;
  1508. regex_t port_regex;
  1509. regex_t type_regex;
  1510. int matching;
  1511. engine = client->engine;
  1512. if (port_name_pattern && port_name_pattern[0]) {
  1513. regcomp (&port_regex, port_name_pattern,
  1514. REG_EXTENDED|REG_NOSUB);
  1515. }
  1516. if (type_name_pattern && type_name_pattern[0]) {
  1517. regcomp (&type_regex, type_name_pattern,
  1518. REG_EXTENDED|REG_NOSUB);
  1519. }
  1520. psp = engine->ports;
  1521. match_cnt = 0;
  1522. matching_ports = (const char **)
  1523. malloc (sizeof (char *) * engine->port_max);
  1524. for (i = 0; i < engine->port_max; i++) {
  1525. matching = 1;
  1526. if (!psp[i].in_use) {
  1527. continue;
  1528. }
  1529. if (flags) {
  1530. if ((psp[i].flags & flags) != flags) {
  1531. matching = 0;
  1532. }
  1533. }
  1534. if (matching && port_name_pattern && port_name_pattern[0]) {
  1535. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1536. matching = 0;
  1537. }
  1538. }
  1539. if (matching && type_name_pattern && type_name_pattern[0]) {
  1540. jack_port_type_id_t ptid = psp[i].ptype_id;
  1541. if (regexec (&type_regex,
  1542. engine->port_types[ptid].type_name,
  1543. 0, NULL, 0)) {
  1544. matching = 0;
  1545. }
  1546. }
  1547. if (matching) {
  1548. matching_ports[match_cnt++] = psp[i].name;
  1549. }
  1550. }
  1551. if (port_name_pattern && port_name_pattern[0]) {
  1552. regfree (&port_regex);
  1553. }
  1554. if (type_name_pattern && type_name_pattern[0]) {
  1555. regfree (&type_regex);
  1556. }
  1557. matching_ports[match_cnt] = 0;
  1558. if (match_cnt == 0) {
  1559. free (matching_ports);
  1560. matching_ports = 0;
  1561. }
  1562. return matching_ports;
  1563. }
  1564. float
  1565. jack_cpu_load (jack_client_t *client)
  1566. {
  1567. return client->engine->cpu_load;
  1568. }
  1569. float
  1570. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1571. {
  1572. return client->engine->xrun_delayed_usecs;
  1573. }
  1574. pthread_t
  1575. jack_client_thread_id (jack_client_t *client)
  1576. {
  1577. return client->thread_id;
  1578. }
  1579. int
  1580. jack_client_name_size(void)
  1581. {
  1582. return JACK_CLIENT_NAME_SIZE;
  1583. }
  1584. int
  1585. jack_port_name_size(void)
  1586. {
  1587. return JACK_PORT_NAME_SIZE;
  1588. }
  1589. int
  1590. jack_port_type_size(void)
  1591. {
  1592. return JACK_PORT_TYPE_SIZE;
  1593. }