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.

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