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.

1897 lines
44KB

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