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.

2843 lines
65KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2005 Jussi Laako
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. #include <limits.h>
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <regex.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. #include <sys/mman.h>
  33. #include <jack/internal.h>
  34. #include <jack/jack.h>
  35. #include <jack/engine.h>
  36. #include <jack/pool.h>
  37. #include <jack/jslist.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/unlock.h>
  41. #include <jack/thread.h>
  42. #include <jack/varargs.h>
  43. #include <jack/intsimd.h>
  44. #include <jack/messagebuffer.h>
  45. #include <sysdeps/time.h>
  46. #include "local.h"
  47. #include <sysdeps/poll.h>
  48. #include <sysdeps/ipc.h>
  49. #include <sysdeps/cycles.h>
  50. #ifdef JACK_USE_MACH_THREADS
  51. #include <sysdeps/pThreadUtilities.h>
  52. #endif
  53. static pthread_mutex_t client_lock;
  54. static pthread_cond_t client_ready;
  55. static int
  56. jack_client_close_aux (jack_client_t *client);
  57. #define EVENT_POLL_INDEX 0
  58. #define WAIT_POLL_INDEX 1
  59. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  60. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
  61. typedef struct {
  62. int status;
  63. struct _jack_client *client;
  64. const char *client_name;
  65. } client_info;
  66. #ifdef USE_DYNSIMD
  67. #ifdef ARCH_X86
  68. int cpu_type = 0;
  69. static void
  70. init_cpu ()
  71. {
  72. cpu_type = ((have_3dnow() << 8) | have_sse());
  73. if (ARCH_X86_HAVE_3DNOW(cpu_type))
  74. jack_info("Enhanced3DNow! detected");
  75. if (ARCH_X86_HAVE_SSE2(cpu_type))
  76. jack_info("SSE2 detected");
  77. if ((!ARCH_X86_HAVE_3DNOW(cpu_type)) && (!ARCH_X86_HAVE_SSE2(cpu_type)))
  78. jack_info("No supported SIMD instruction sets detected");
  79. jack_port_set_funcs();
  80. }
  81. #else /* ARCH_X86 */
  82. static void
  83. init_cpu ()
  84. {
  85. jack_port_set_funcs();
  86. }
  87. #endif /* ARCH_X86 */
  88. #endif /* USE_DYNSIMD */
  89. char *jack_tmpdir = DEFAULT_TMP_DIR;
  90. static int
  91. jack_get_tmpdir ()
  92. {
  93. FILE* in;
  94. size_t len;
  95. char buf[PATH_MAX+2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  96. char *pathenv;
  97. char *pathcopy;
  98. char *p;
  99. /* some implementations of popen(3) close a security loophole by
  100. resetting PATH for the exec'd command. since we *want* to
  101. use the user's PATH setting to locate jackd, we have to
  102. do it ourselves.
  103. */
  104. if ((pathenv = getenv ("PATH")) == 0) {
  105. return -1;
  106. }
  107. /* don't let strtok(3) mess with the real environment variable */
  108. if ((pathcopy = strdup (pathenv)) == NULL) {
  109. return -1;
  110. }
  111. p = strtok (pathcopy, ":");
  112. while (p) {
  113. char jackd[PATH_MAX+1];
  114. char command[PATH_MAX+4];
  115. snprintf (jackd, sizeof (jackd), "%s/jackd", p);
  116. if (access (jackd, X_OK) == 0) {
  117. snprintf (command, sizeof (command), "%s -l", jackd);
  118. if ((in = popen (command, "r")) != NULL) {
  119. break;
  120. }
  121. }
  122. p = strtok (NULL, ":");
  123. }
  124. if (p == NULL) {
  125. /* no command successfully started */
  126. free (pathcopy);
  127. return -1;
  128. }
  129. if (fgets (buf, sizeof (buf), in) == NULL) {
  130. fclose (in);
  131. free (pathcopy);
  132. return -1;
  133. }
  134. len = strlen (buf);
  135. if (buf[len-1] != '\n') {
  136. /* didn't get a whole line */
  137. fclose (in);
  138. free (pathcopy);
  139. return -1;
  140. }
  141. if ((jack_tmpdir = (char *) malloc (len)) == NULL) {
  142. free (pathcopy);
  143. return -1;
  144. }
  145. memcpy (jack_tmpdir, buf, len-1);
  146. jack_tmpdir[len-1] = '\0';
  147. fclose (in);
  148. free (pathcopy);
  149. return 0;
  150. }
  151. void
  152. jack_error (const char *fmt, ...)
  153. {
  154. va_list ap;
  155. char buffer[300];
  156. va_start (ap, fmt);
  157. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  158. jack_error_callback (buffer);
  159. va_end (ap);
  160. }
  161. void
  162. default_jack_error_callback (const char *desc)
  163. {
  164. #ifdef DEBUG_ENABLED
  165. DEBUG("%s", desc);
  166. #else
  167. fprintf(stderr, "%s\n", desc);
  168. fflush(stderr);
  169. #endif
  170. }
  171. void
  172. default_jack_info_callback (const char *desc)
  173. {
  174. fprintf(stdout, "%s\n", desc);
  175. fflush(stdout);
  176. }
  177. void
  178. silent_jack_error_callback (const char *desc)
  179. {
  180. }
  181. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  182. void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;
  183. void
  184. jack_info (const char *fmt, ...)
  185. {
  186. va_list ap;
  187. char buffer[300];
  188. va_start (ap, fmt);
  189. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  190. jack_info_callback (buffer);
  191. va_end (ap);
  192. }
  193. static int
  194. oop_client_deliver_request (void *ptr, jack_request_t *req)
  195. {
  196. int wok, rok;
  197. jack_client_t *client = (jack_client_t*) ptr;
  198. wok = (write (client->request_fd, req, sizeof (*req))
  199. == sizeof (*req));
  200. rok = (read (client->request_fd, req, sizeof (*req))
  201. == sizeof (*req));
  202. if (wok && rok) { /* everything OK? */
  203. return req->status;
  204. }
  205. req->status = -1; /* request failed */
  206. /* check for server shutdown */
  207. if (client->engine->engine_ok == 0)
  208. return req->status;
  209. /* otherwise report errors */
  210. if (!wok)
  211. jack_error ("cannot send request type %d to server",
  212. req->type);
  213. if (!rok)
  214. jack_error ("cannot read result for request type %d from"
  215. " server (%s)", req->type, strerror (errno));
  216. return req->status;
  217. }
  218. int
  219. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  220. {
  221. /* indirect through the function pointer that was set either
  222. * by jack_client_open() or by jack_new_client_request() in
  223. * the server.
  224. */
  225. return client->deliver_request (client->deliver_arg,
  226. req);
  227. }
  228. #if JACK_USE_MACH_THREADS
  229. jack_client_t *
  230. jack_client_alloc ()
  231. {
  232. jack_client_t *client;
  233. if ((client = (jack_client_t *) malloc (sizeof (jack_client_t))) == NULL) {
  234. return NULL;
  235. }
  236. if ((client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 1)) == NULL) {
  237. free (client);
  238. return NULL;
  239. }
  240. client->pollmax = 1;
  241. client->request_fd = -1;
  242. client->event_fd = -1;
  243. client->upstream_is_jackd = 0;
  244. client->graph_next_fd = -1;
  245. client->ports = NULL;
  246. client->ports_ext = NULL;
  247. client->engine = NULL;
  248. client->control = NULL;
  249. client->thread_ok = FALSE;
  250. client->rt_thread_ok = FALSE;
  251. client->first_active = TRUE;
  252. client->on_shutdown = NULL;
  253. client->on_info_shutdown = NULL;
  254. client->n_port_types = 0;
  255. client->port_segment = NULL;
  256. #ifdef USE_DYNSIMD
  257. init_cpu();
  258. #endif /* USE_DYNSIMD */
  259. return client;
  260. }
  261. #else
  262. jack_client_t *
  263. jack_client_alloc ()
  264. {
  265. jack_client_t *client;
  266. if ((client = (jack_client_t *) malloc (sizeof (jack_client_t))) == NULL) {
  267. return NULL;
  268. }
  269. if ((client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2)) == NULL) {
  270. free (client);
  271. return NULL;
  272. }
  273. client->pollmax = 2;
  274. client->request_fd = -1;
  275. client->event_fd = -1;
  276. client->upstream_is_jackd = 0;
  277. client->graph_wait_fd = -1;
  278. client->graph_next_fd = -1;
  279. client->ports = NULL;
  280. client->ports_ext = NULL;
  281. client->engine = NULL;
  282. client->control = NULL;
  283. client->thread_ok = FALSE;
  284. client->first_active = TRUE;
  285. client->on_shutdown = NULL;
  286. client->on_info_shutdown = NULL;
  287. client->n_port_types = 0;
  288. client->port_segment = NULL;
  289. #ifdef USE_DYNSIMD
  290. init_cpu();
  291. #endif /* USE_DYNSIMD */
  292. return client;
  293. }
  294. #endif
  295. /*
  296. * Build the jack_client_t structure for an internal client.
  297. */
  298. jack_client_t *
  299. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  300. {
  301. jack_client_t* client;
  302. client = jack_client_alloc ();
  303. client->control = cc;
  304. client->engine = engine->control;
  305. client->n_port_types = client->engine->n_port_types;
  306. client->port_segment = &engine->port_segment[0];
  307. return client;
  308. }
  309. static void
  310. jack_client_free (jack_client_t *client)
  311. {
  312. if (client->pollfd) {
  313. free (client->pollfd);
  314. }
  315. free (client);
  316. }
  317. void
  318. jack_client_fix_port_buffers (jack_client_t *client)
  319. {
  320. JSList *node;
  321. jack_port_t *port;
  322. /* This releases all local memory owned by input ports
  323. and sets the buffer pointer to NULL. This will cause
  324. jack_port_get_buffer() to reallocate space for the
  325. buffer on the next call (if there is one).
  326. */
  327. for (node = client->ports; node; node = jack_slist_next (node)) {
  328. port = (jack_port_t *) node->data;
  329. if (port->shared->flags & JackPortIsInput) {
  330. if (port->mix_buffer) {
  331. size_t buffer_size =
  332. jack_port_type_buffer_size( port->type_info,
  333. client->engine->buffer_size );
  334. jack_pool_release (port->mix_buffer);
  335. port->mix_buffer = NULL;
  336. pthread_mutex_lock (&port->connection_lock);
  337. if (jack_slist_length (port->connections) > 1) {
  338. port->mix_buffer = jack_pool_alloc (buffer_size);
  339. port->fptr.buffer_init (port->mix_buffer,
  340. buffer_size,
  341. client->engine->buffer_size);
  342. }
  343. pthread_mutex_unlock (&port->connection_lock);
  344. }
  345. }
  346. }
  347. }
  348. int
  349. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  350. {
  351. jack_port_t *control_port;
  352. jack_port_t *other = 0;
  353. JSList *node;
  354. int need_free = FALSE;
  355. if (client->engine->ports[event->x.self_id].client_id == client->control->id ||
  356. client->engine->ports[event->y.other_id].client_id == client->control->id) {
  357. /* its one of ours */
  358. switch (event->type) {
  359. case PortConnected:
  360. other = jack_port_new (client, event->y.other_id,
  361. client->engine);
  362. /* jack_port_by_id_int() always returns an internal
  363. * port that does not need to be deallocated
  364. */
  365. control_port = jack_port_by_id_int (client, event->x.self_id,
  366. &need_free);
  367. pthread_mutex_lock (&control_port->connection_lock);
  368. if ((control_port->shared->flags & JackPortIsInput)
  369. && (control_port->connections != NULL)
  370. && (control_port->mix_buffer == NULL) ) {
  371. size_t buffer_size =
  372. jack_port_type_buffer_size( control_port->type_info,
  373. client->engine->buffer_size );
  374. control_port->mix_buffer = jack_pool_alloc (buffer_size);
  375. control_port->fptr.buffer_init (control_port->mix_buffer,
  376. buffer_size,
  377. client->engine->buffer_size);
  378. }
  379. control_port->connections =
  380. jack_slist_prepend (control_port->connections,
  381. (void *) other);
  382. pthread_mutex_unlock (&control_port->connection_lock);
  383. break;
  384. case PortDisconnected:
  385. /* jack_port_by_id_int() always returns an internal
  386. * port that does not need to be deallocated
  387. */
  388. control_port = jack_port_by_id_int (client, event->x.self_id,
  389. &need_free);
  390. pthread_mutex_lock (&control_port->connection_lock);
  391. for (node = control_port->connections; node;
  392. node = jack_slist_next (node)) {
  393. other = (jack_port_t *) node->data;
  394. if (other->shared->id == event->y.other_id) {
  395. control_port->connections =
  396. jack_slist_remove_link (
  397. control_port->connections,
  398. node);
  399. jack_slist_free_1 (node);
  400. free (other);
  401. break;
  402. }
  403. }
  404. pthread_mutex_unlock (&control_port->connection_lock);
  405. break;
  406. default:
  407. /* impossible */
  408. break;
  409. }
  410. }
  411. if (client->control->port_connect_cbset) {
  412. client->port_connect (event->x.self_id, event->y.other_id,
  413. (event->type == PortConnected ? 1 : 0),
  414. client->port_connect_arg);
  415. }
  416. return 0;
  417. }
  418. int
  419. jack_client_handle_session_callback (jack_client_t *client, jack_event_t *event)
  420. {
  421. char prefix[32];
  422. jack_session_event_t *s_event;
  423. if (! client->control->session_cbset) {
  424. return -1;
  425. }
  426. snprintf( prefix, sizeof(prefix), "%d", client->control->uid );
  427. s_event = malloc( sizeof(jack_session_event_t) );
  428. s_event->type = event->y.n;
  429. s_event->session_dir = strdup( event->x.name );
  430. s_event->client_uuid = strdup( prefix );
  431. s_event->command_line = NULL;
  432. client->session_cb_immediate_reply = 0;
  433. client->session_cb ( s_event, client->session_cb_arg);
  434. if (client->session_cb_immediate_reply) {
  435. return 2;
  436. }
  437. return 1;
  438. }
  439. #if JACK_USE_MACH_THREADS
  440. static int
  441. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  442. {
  443. client->pollmax = 1;
  444. /* If the client registered its own callback for graph order events,
  445. execute it now.
  446. */
  447. if (client->control->graph_order_cbset) {
  448. client->graph_order (client->graph_order_arg);
  449. }
  450. return 0;
  451. }
  452. #else
  453. static int
  454. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  455. {
  456. char path[PATH_MAX+1];
  457. DEBUG ("graph reorder\n");
  458. if (client->graph_wait_fd >= 0) {
  459. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  460. close (client->graph_wait_fd);
  461. client->graph_wait_fd = -1;
  462. }
  463. if (client->graph_next_fd >= 0) {
  464. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  465. close (client->graph_next_fd);
  466. client->graph_next_fd = -1;
  467. }
  468. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  469. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  470. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  471. path, strerror (errno));
  472. return -1;
  473. }
  474. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  475. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  476. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  477. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  478. path, strerror (errno));
  479. return -1;
  480. }
  481. client->upstream_is_jackd = event->y.n;
  482. client->pollmax = 2;
  483. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  484. client->graph_next_fd, path,
  485. client->upstream_is_jackd);
  486. /* If the client registered its own callback for graph order events,
  487. execute it now.
  488. */
  489. if (client->control->graph_order_cbset) {
  490. client->graph_order (client->graph_order_arg);
  491. }
  492. return 0;
  493. }
  494. #endif
  495. static int
  496. server_connect (const char *server_name)
  497. {
  498. int fd;
  499. struct sockaddr_un addr;
  500. int which = 0;
  501. char server_dir[PATH_MAX+1] = "";
  502. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  503. jack_error ("cannot create client socket (%s)",
  504. strerror (errno));
  505. return -1;
  506. }
  507. //JOQ: temporary debug message
  508. //jack_info ("DEBUG: connecting to `%s' server", server_name);
  509. addr.sun_family = AF_UNIX;
  510. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  511. jack_server_dir (server_name, server_dir) , which);
  512. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  513. close (fd);
  514. return -1;
  515. }
  516. return fd;
  517. }
  518. static int
  519. server_event_connect (jack_client_t *client, const char *server_name)
  520. {
  521. int fd;
  522. struct sockaddr_un addr;
  523. jack_client_connect_ack_request_t req;
  524. jack_client_connect_ack_result_t res;
  525. char server_dir[PATH_MAX+1] = "";
  526. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  527. jack_error ("cannot create client event socket (%s)",
  528. strerror (errno));
  529. return -1;
  530. }
  531. addr.sun_family = AF_UNIX;
  532. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  533. jack_server_dir (server_name,server_dir));
  534. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  535. jack_error ("cannot connect to jack server for events",
  536. strerror (errno));
  537. close (fd);
  538. return -1;
  539. }
  540. req.client_id = client->control->id;
  541. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  542. jack_error ("cannot write event connect request to server (%s)",
  543. strerror (errno));
  544. close (fd);
  545. return -1;
  546. }
  547. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  548. jack_error ("cannot read event connect result from server (%s)",
  549. strerror (errno));
  550. close (fd);
  551. return -1;
  552. }
  553. if (res.status != 0) {
  554. jack_error ("cannot connect to server for event stream (%s)",
  555. strerror (errno));
  556. close (fd);
  557. return -1;
  558. }
  559. return fd;
  560. }
  561. /* Exec the JACK server in this process. Does not return. */
  562. static void
  563. _start_server (const char *server_name)
  564. {
  565. FILE* fp = 0;
  566. char filename[255];
  567. char arguments[255];
  568. char buffer[255];
  569. char* command = 0;
  570. size_t pos = 0;
  571. size_t result = 0;
  572. char** argv = 0;
  573. int i = 0;
  574. int good = 0;
  575. int ret;
  576. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  577. fp = fopen(filename, "r");
  578. if (!fp) {
  579. fp = fopen("/etc/jackdrc", "r");
  580. }
  581. /* if still not found, check old config name for backwards compatability */
  582. if (!fp) {
  583. fp = fopen("/etc/jackd.conf", "r");
  584. }
  585. if (fp) {
  586. arguments[0] = '\0';
  587. ret = fscanf(fp, "%s", buffer);
  588. while(ret != 0 && ret != EOF) {
  589. strcat(arguments, buffer);
  590. strcat(arguments, " ");
  591. ret = fscanf(fp, "%s", buffer);
  592. }
  593. if (strlen(arguments) > 0) {
  594. good = 1;
  595. }
  596. }
  597. if (!good) {
  598. #if defined(USE_CAPABILITIES)
  599. command = JACK_LOCATION "/jackstart";
  600. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  601. JACK_DEFAULT_DRIVER " -p 512", 255);
  602. #else /* !USE_CAPABILITIES */
  603. command = JACK_LOCATION "/jackd";
  604. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  605. JACK_DEFAULT_DRIVER, 255);
  606. #endif /* USE_CAPABILITIES */
  607. } else {
  608. result = strcspn(arguments, " ");
  609. if ((command = (char *) malloc(result+1)) == NULL) {
  610. goto failure;
  611. }
  612. strncpy(command, arguments, result);
  613. command[result] = '\0';
  614. }
  615. if ((argv = (char **) malloc (255)) == NULL) {
  616. goto failure;
  617. }
  618. while(1) {
  619. /* insert -T and -nserver_name in front of arguments */
  620. if (i == 1) {
  621. argv[i] = (char *) malloc(strlen ("-T") + 1);
  622. strcpy (argv[i++], "-T");
  623. if (server_name) {
  624. size_t optlen = strlen ("-n");
  625. char *buf =
  626. malloc (optlen
  627. + strlen (server_name) + 1);
  628. strcpy (buf, "-n");
  629. strcpy (buf+optlen, server_name);
  630. argv[i++] = buf;
  631. }
  632. }
  633. result = strcspn(arguments+pos, " ");
  634. if (result == 0) {
  635. break;
  636. }
  637. argv[i] = (char*)malloc(result+1);
  638. strncpy(argv[i], arguments+pos, result);
  639. argv[i][result] = '\0';
  640. pos += result+1;
  641. ++i;
  642. }
  643. argv[i] = 0;
  644. #if 0
  645. fprintf (stderr, "execing JACK using %s\n", command);
  646. for (_xx = 0; argv[_xx]; ++_xx) {
  647. fprintf (stderr, "\targv[%d] = %s\n", _xx, argv[_xx]);
  648. }
  649. #endif
  650. execv (command, argv);
  651. failure:
  652. /* If execv() succeeds, it does not return. There's no point
  653. * in calling jack_error() here in the child process. */
  654. fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
  655. }
  656. int
  657. start_server (const char *server_name, jack_options_t options)
  658. {
  659. if ((options & JackNoStartServer)
  660. || getenv("JACK_NO_START_SERVER")) {
  661. return 1;
  662. }
  663. /* The double fork() forces the server to become a child of
  664. * init, which will always clean up zombie process state on
  665. * termination. This even works in cases where the server
  666. * terminates but this client does not.
  667. *
  668. * Since fork() is usually implemented using copy-on-write
  669. * virtual memory tricks, the overhead of the second fork() is
  670. * probably relatively small.
  671. */
  672. switch (fork()) {
  673. case 0: /* child process */
  674. switch (fork()) {
  675. case 0: /* grandchild process */
  676. _start_server(server_name);
  677. _exit (99); /* exec failed */
  678. case -1:
  679. _exit (98);
  680. default:
  681. _exit (0);
  682. }
  683. case -1: /* fork() error */
  684. return 1; /* failed to start server */
  685. }
  686. /* only the original parent process goes here */
  687. return 0; /* (probably) successful */
  688. }
  689. static int
  690. jack_request_client (ClientType type,
  691. const char* client_name, jack_options_t options,
  692. jack_status_t *status, jack_varargs_t *va,
  693. jack_client_connect_result_t *res, int *req_fd)
  694. {
  695. jack_client_connect_request_t req;
  696. *req_fd = -1;
  697. memset (&req, 0, sizeof (req));
  698. req.options = options;
  699. if (strlen (client_name) >= sizeof (req.name)) {
  700. jack_error ("\"%s\" is too long to be used as a JACK client"
  701. " name.\n"
  702. "Please use %lu characters or less.",
  703. client_name, sizeof (req.name));
  704. return -1;
  705. }
  706. if (va->load_name
  707. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  708. jack_error ("\"%s\" is too long to be used as a JACK shared"
  709. " object name.\n"
  710. "Please use %lu characters or less.",
  711. va->load_name, sizeof (req.object_path) - 1);
  712. return -1;
  713. }
  714. if (va->load_init
  715. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  716. jack_error ("\"%s\" is too long to be used as a JACK shared"
  717. " object data string.\n"
  718. "Please use %lu characters or less.",
  719. va->load_init, sizeof (req.object_data) - 1);
  720. return -1;
  721. }
  722. if ((*req_fd = server_connect (va->server_name)) < 0) {
  723. int trys;
  724. if (start_server(va->server_name, options)) {
  725. *status |= (JackFailure|JackServerFailed);
  726. goto fail;
  727. }
  728. trys = 5;
  729. do {
  730. sleep(1);
  731. if (--trys < 0) {
  732. *status |= (JackFailure|JackServerFailed);
  733. goto fail;
  734. }
  735. } while ((*req_fd = server_connect (va->server_name)) < 0);
  736. *status |= JackServerStarted;
  737. }
  738. /* format connection request */
  739. if( va->sess_uuid )
  740. req.uuid = atoi( va->sess_uuid );
  741. else
  742. req.uuid = 0;
  743. req.protocol_v = jack_protocol_version;
  744. req.load = TRUE;
  745. req.type = type;
  746. snprintf (req.name, sizeof (req.name),
  747. "%s", client_name);
  748. snprintf (req.object_path, sizeof (req.object_path),
  749. "%s", va->load_name);
  750. snprintf (req.object_data, sizeof (req.object_data),
  751. "%s", va->load_init);
  752. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  753. jack_error ("cannot send request to jack server (%s)",
  754. strerror (errno));
  755. *status |= (JackFailure|JackServerError);
  756. goto fail;
  757. }
  758. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  759. if (errno == 0) {
  760. /* server shut the socket */
  761. jack_error ("could not attach as client");
  762. *status |= (JackFailure|JackServerError);
  763. goto fail;
  764. }
  765. if (errno == ECONNRESET) {
  766. jack_error ("could not attach as JACK client "
  767. "(server has exited)");
  768. *status |= (JackFailure|JackServerError);
  769. goto fail;
  770. }
  771. jack_error ("cannot read response from jack server (%s)",
  772. strerror (errno));
  773. *status |= (JackFailure|JackServerError);
  774. goto fail;
  775. }
  776. *status |= res->status; /* return server status bits */
  777. if (*status & JackFailure) {
  778. if (*status & JackVersionError) {
  779. jack_error ("client linked with incompatible libjack"
  780. " version.");
  781. }
  782. jack_error ("could not attach to JACK server");
  783. *status |= JackServerError;
  784. goto fail;
  785. }
  786. switch (type) {
  787. case ClientDriver:
  788. case ClientInternal:
  789. close (*req_fd);
  790. *req_fd = -1;
  791. break;
  792. default:
  793. break;
  794. }
  795. return 0;
  796. fail:
  797. if (*req_fd >= 0) {
  798. close (*req_fd);
  799. *req_fd = -1;
  800. }
  801. return -1;
  802. }
  803. int
  804. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  805. {
  806. /* Lookup, attach and register the port/buffer segments in use
  807. * right now.
  808. */
  809. if (client->control->type != ClientExternal) {
  810. jack_error("Only external clients need attach port segments");
  811. abort();
  812. }
  813. /* make sure we have space to store the port
  814. segment information.
  815. */
  816. if (ptid >= client->n_port_types) {
  817. client->port_segment = (jack_shm_info_t*)
  818. realloc (client->port_segment,
  819. sizeof (jack_shm_info_t) * (ptid+1));
  820. memset (&client->port_segment[client->n_port_types],
  821. 0,
  822. sizeof (jack_shm_info_t) *
  823. (ptid - client->n_port_types));
  824. client->n_port_types = ptid + 1;
  825. } else {
  826. /* release any previous segment */
  827. jack_release_shm (&client->port_segment[ptid]);
  828. }
  829. /* get the index into the shm registry */
  830. client->port_segment[ptid].index =
  831. client->engine->port_types[ptid].shm_registry_index;
  832. /* attach the relevant segment */
  833. if (jack_attach_shm (&client->port_segment[ptid])) {
  834. jack_error ("cannot attach port segment shared memory"
  835. " (%s)", strerror (errno));
  836. return -1;
  837. }
  838. return 0;
  839. }
  840. jack_client_t *
  841. jack_client_open_aux (const char *client_name,
  842. jack_options_t options,
  843. jack_status_t *status, va_list ap)
  844. {
  845. /* optional arguments: */
  846. jack_varargs_t va; /* variable arguments */
  847. int req_fd = -1;
  848. int ev_fd = -1;
  849. jack_client_connect_result_t res;
  850. jack_client_t *client;
  851. jack_port_type_id_t ptid;
  852. jack_status_t my_status;
  853. jack_messagebuffer_init ();
  854. if (status == NULL) /* no status from caller? */
  855. status = &my_status; /* use local status word */
  856. *status = 0;
  857. /* validate parameters */
  858. if ((options & ~JackOpenOptions)) {
  859. *status |= (JackFailure|JackInvalidOption);
  860. jack_messagebuffer_exit ();
  861. return NULL;
  862. }
  863. /* parse variable arguments */
  864. jack_varargs_parse(options, ap, &va);
  865. /* External clients need to know where the tmpdir used for
  866. communication with the server lives
  867. */
  868. if (jack_get_tmpdir ()) {
  869. *status |= JackFailure;
  870. jack_messagebuffer_exit ();
  871. return NULL;
  872. }
  873. /* External clients need this initialized. It is already set
  874. * up in the server's address space for internal clients.
  875. */
  876. jack_init_time ();
  877. if (jack_request_client (ClientExternal, client_name, options, status,
  878. &va, &res, &req_fd)) {
  879. jack_messagebuffer_exit ();
  880. return NULL;
  881. }
  882. /* Allocate the jack_client_t structure in local memory.
  883. * Shared memory is not accessible yet. */
  884. client = jack_client_alloc ();
  885. strcpy (client->name, res.name);
  886. strcpy (client->fifo_prefix, res.fifo_prefix);
  887. client->request_fd = req_fd;
  888. client->pollfd[EVENT_POLL_INDEX].events =
  889. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  890. #ifndef JACK_USE_MACH_THREADS
  891. client->pollfd[WAIT_POLL_INDEX].events =
  892. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  893. #endif
  894. /* Don't access shared memory until server connected. */
  895. if (jack_initialize_shm (va.server_name)) {
  896. jack_error ("Unable to initialize shared memory.");
  897. *status |= (JackFailure|JackShmFailure);
  898. goto fail;
  899. }
  900. /* attach the engine control/info block */
  901. client->engine_shm.index = res.engine_shm_index;
  902. if (jack_attach_shm (&client->engine_shm)) {
  903. jack_error ("cannot attached engine control shared memory"
  904. " segment");
  905. goto fail;
  906. }
  907. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  908. /* initialize clock source as early as possible */
  909. jack_set_clock_source (client->engine->clock_source);
  910. /* now attach the client control block */
  911. client->control_shm.index = res.client_shm_index;
  912. if (jack_attach_shm (&client->control_shm)) {
  913. jack_error ("cannot attached client control shared memory"
  914. " segment");
  915. goto fail;
  916. }
  917. client->control = (jack_client_control_t *)
  918. jack_shm_addr (&client->control_shm);
  919. /* Nobody else needs to access this shared memory any more, so
  920. * destroy it. Because we have it attached, it won't vanish
  921. * till we exit (and release it).
  922. */
  923. jack_destroy_shm (&client->control_shm);
  924. client->n_port_types = client->engine->n_port_types;
  925. if ((client->port_segment = (jack_shm_info_t *) malloc (sizeof (jack_shm_info_t) * client->n_port_types)) == NULL) {
  926. goto fail;
  927. }
  928. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  929. client->port_segment[ptid].index =
  930. client->engine->port_types[ptid].shm_registry_index;
  931. client->port_segment[ptid].attached_at = MAP_FAILED;
  932. jack_attach_port_segment (client, ptid);
  933. }
  934. /* set up the client so that it does the right thing for an
  935. * external client
  936. */
  937. client->deliver_request = oop_client_deliver_request;
  938. client->deliver_arg = client;
  939. if( va.sess_uuid )
  940. client->control->uid = atoi( va.sess_uuid );
  941. else
  942. client->control->uid = 0U;
  943. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  944. goto fail;
  945. }
  946. client->event_fd = ev_fd;
  947. #ifdef JACK_USE_MACH_THREADS
  948. /* specific resources for server/client real-time thread
  949. * communication */
  950. client->clienttask = mach_task_self();
  951. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  952. jack_error ("Can't find bootstrap port");
  953. goto fail;
  954. }
  955. if (allocate_mach_clientport(client, res.portnum) < 0) {
  956. jack_error("Can't allocate mach port");
  957. goto fail;
  958. };
  959. #endif /* JACK_USE_MACH_THREADS */
  960. return client;
  961. fail:
  962. jack_messagebuffer_exit ();
  963. if (client->engine) {
  964. jack_release_shm (&client->engine_shm);
  965. client->engine = 0;
  966. }
  967. if (client->control) {
  968. jack_release_shm (&client->control_shm);
  969. client->control = 0;
  970. }
  971. if (req_fd >= 0) {
  972. close (req_fd);
  973. }
  974. if (ev_fd >= 0) {
  975. close (ev_fd);
  976. }
  977. free (client);
  978. return NULL;
  979. }
  980. jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  981. {
  982. va_list ap;
  983. va_start(ap, status);
  984. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  985. va_end(ap);
  986. return res;
  987. }
  988. jack_client_t *
  989. jack_client_new (const char *client_name)
  990. {
  991. jack_options_t options = JackUseExactName;
  992. if (getenv("JACK_START_SERVER") == NULL)
  993. options |= JackNoStartServer;
  994. return jack_client_open (client_name, options, NULL);
  995. }
  996. char *
  997. jack_get_client_name (jack_client_t *client)
  998. {
  999. return client->name;
  1000. }
  1001. int
  1002. jack_internal_client_new (const char *client_name,
  1003. const char *so_name, const char *so_data)
  1004. {
  1005. jack_client_connect_result_t res;
  1006. int req_fd;
  1007. jack_varargs_t va;
  1008. jack_status_t status;
  1009. jack_options_t options = JackUseExactName;
  1010. if (getenv("JACK_START_SERVER") == NULL)
  1011. options |= JackNoStartServer;
  1012. jack_varargs_init (&va);
  1013. va.load_name = (char *) so_name;
  1014. va.load_init = (char *) so_data;
  1015. return jack_request_client (ClientInternal, client_name,
  1016. options, &status, &va, &res, &req_fd);
  1017. }
  1018. char *
  1019. jack_default_server_name (void)
  1020. {
  1021. char *server_name;
  1022. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  1023. server_name = "default";
  1024. return server_name;
  1025. }
  1026. /* returns the name of the per-user subdirectory of jack_tmpdir */
  1027. char *
  1028. jack_user_dir (void)
  1029. {
  1030. static char user_dir[PATH_MAX+1] = "";
  1031. /* format the path name on the first call */
  1032. if (user_dir[0] == '\0') {
  1033. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  1034. snprintf (user_dir, sizeof (user_dir), "%s/jack",
  1035. jack_tmpdir);
  1036. } else {
  1037. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  1038. jack_tmpdir, getuid ());
  1039. }
  1040. }
  1041. return user_dir;
  1042. }
  1043. /* returns the name of the per-server subdirectory of jack_user_dir() */
  1044. char *
  1045. jack_server_dir (const char *server_name, char *server_dir)
  1046. {
  1047. /* format the path name into the suppled server_dir char array,
  1048. * assuming that server_dir is at least as large as PATH_MAX+1 */
  1049. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  1050. jack_user_dir (), server_name);
  1051. return server_dir;
  1052. }
  1053. void
  1054. jack_internal_client_close (const char *client_name)
  1055. {
  1056. jack_client_connect_request_t req;
  1057. int fd;
  1058. char *server_name = jack_default_server_name ();
  1059. req.load = FALSE;
  1060. snprintf (req.name, sizeof (req.name), "%s", client_name);
  1061. if ((fd = server_connect (server_name)) < 0) {
  1062. return;
  1063. }
  1064. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  1065. jack_error ("cannot deliver ClientUnload request to JACK "
  1066. "server.");
  1067. }
  1068. /* no response to this request */
  1069. close (fd);
  1070. return;
  1071. }
  1072. int
  1073. jack_recompute_total_latencies (jack_client_t* client)
  1074. {
  1075. jack_request_t request;
  1076. request.type = RecomputeTotalLatencies;
  1077. return jack_client_deliver_request (client, &request);
  1078. }
  1079. int
  1080. jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
  1081. {
  1082. jack_request_t request;
  1083. request.type = RecomputeTotalLatency;
  1084. request.x.port_info.port_id = port->shared->id;
  1085. return jack_client_deliver_request (client, &request);
  1086. }
  1087. int
  1088. jack_set_freewheel (jack_client_t* client, int onoff)
  1089. {
  1090. jack_request_t request;
  1091. request.type = onoff ? FreeWheel : StopFreeWheel;
  1092. request.x.client_id = client->control->id;
  1093. return jack_client_deliver_request (client, &request);
  1094. }
  1095. int
  1096. jack_session_reply (jack_client_t *client, jack_session_event_t *event )
  1097. {
  1098. int retval = 0;
  1099. if (event->command_line) {
  1100. snprintf ((char *)client->control->session_command,
  1101. sizeof(client->control->session_command),
  1102. "%s", event->command_line);
  1103. client->control->session_flags = event->flags;
  1104. } else {
  1105. retval = -1;
  1106. }
  1107. if (pthread_self() == client->thread_id) {
  1108. client->session_cb_immediate_reply = 1;
  1109. } else {
  1110. jack_request_t request;
  1111. request.type = SessionReply;
  1112. request.x.client_id = client->control->id;
  1113. retval = jack_client_deliver_request(client, &request);
  1114. }
  1115. return retval;
  1116. }
  1117. void
  1118. jack_session_event_free (jack_session_event_t *event)
  1119. {
  1120. if (event->command_line)
  1121. free (event->command_line);
  1122. free ((char *)event->session_dir);
  1123. free ((char *)event->client_uuid);
  1124. free (event);
  1125. }
  1126. void
  1127. jack_session_commands_free (jack_session_command_t *cmds)
  1128. {
  1129. int i=0;
  1130. while(1) {
  1131. if (cmds[i].client_name)
  1132. free ((char *)cmds[i].client_name);
  1133. if (cmds[i].command)
  1134. free ((char *)cmds[i].command);
  1135. if (cmds[i].uuid)
  1136. free ((char *)cmds[i].uuid);
  1137. else
  1138. break;
  1139. i += 1;
  1140. }
  1141. free(cmds);
  1142. }
  1143. jack_session_command_t *
  1144. jack_session_notify (jack_client_t* client, const char *target, jack_session_event_type_t code, const char *path )
  1145. {
  1146. jack_request_t request;
  1147. jack_session_command_t *retval = NULL;
  1148. int num_replies = 0;
  1149. request.type = SessionNotify;
  1150. if( path )
  1151. snprintf( request.x.session.path, sizeof( request.x.session.path ), "%s", path );
  1152. else
  1153. request.x.session.path[0] = '\0';
  1154. if( target )
  1155. snprintf( request.x.session.target, sizeof( request.x.session.target ), "%s", target );
  1156. else
  1157. request.x.session.target[0] = '\0';
  1158. request.x.session.type = code;
  1159. if( (write (client->request_fd, &request, sizeof (request))
  1160. != sizeof (request)) ) {
  1161. jack_error ("cannot send request type %d to server",
  1162. request.type);
  1163. goto out;
  1164. }
  1165. while( 1 ) {
  1166. jack_client_id_t uid;
  1167. if (read (client->request_fd, &uid, sizeof (uid)) != sizeof (uid)) {
  1168. jack_error ("cannot read result for request type %d from"
  1169. " server (%s)", request.type, strerror (errno));
  1170. goto out;
  1171. }
  1172. num_replies += 1;
  1173. retval = realloc( retval, (num_replies)*sizeof(jack_session_command_t) );
  1174. retval[num_replies-1].client_name = malloc (JACK_CLIENT_NAME_SIZE);
  1175. retval[num_replies-1].command = malloc (JACK_PORT_NAME_SIZE);
  1176. retval[num_replies-1].uuid = malloc (16);
  1177. if ( (retval[num_replies-1].client_name == NULL)
  1178. ||(retval[num_replies-1].command == NULL)
  1179. ||(retval[num_replies-1].uuid == NULL) )
  1180. goto out;
  1181. if( uid == 0 )
  1182. break;
  1183. if (read (client->request_fd, (char *)retval[num_replies-1].client_name, JACK_CLIENT_NAME_SIZE)
  1184. != JACK_CLIENT_NAME_SIZE) {
  1185. jack_error ("cannot read result for request type %d from"
  1186. " server (%s)", request.type, strerror (errno));
  1187. goto out;
  1188. }
  1189. if (read (client->request_fd, (char *)retval[num_replies-1].command, JACK_PORT_NAME_SIZE)
  1190. != JACK_PORT_NAME_SIZE) {
  1191. jack_error ("cannot read result for request type %d from"
  1192. " server (%s)", request.type, strerror (errno));
  1193. goto out;
  1194. }
  1195. if (read (client->request_fd, & retval[num_replies-1].flags, sizeof(retval[num_replies-1].flags) )
  1196. != sizeof(retval[num_replies-1].flags) ) {
  1197. jack_error ("cannot read result for request type %d from"
  1198. " server (%s)", request.type, strerror (errno));
  1199. goto out;
  1200. }
  1201. snprintf( (char *)retval[num_replies-1].uuid, 16, "%d", uid );
  1202. }
  1203. free((char *)retval[num_replies-1].uuid);
  1204. retval[num_replies-1].uuid = NULL;
  1205. return retval;
  1206. out:
  1207. if( retval )
  1208. jack_session_commands_free(retval);
  1209. return NULL;
  1210. }
  1211. void
  1212. jack_start_freewheel (jack_client_t* client)
  1213. {
  1214. jack_client_control_t *control = client->control;
  1215. if (client->engine->real_time) {
  1216. #if JACK_USE_MACH_THREADS
  1217. jack_drop_real_time_scheduling (client->process_thread);
  1218. #else
  1219. jack_drop_real_time_scheduling (client->thread);
  1220. #endif
  1221. }
  1222. if (control->freewheel_cb_cbset) {
  1223. client->freewheel_cb (1, client->freewheel_arg);
  1224. }
  1225. }
  1226. void
  1227. jack_stop_freewheel (jack_client_t* client)
  1228. {
  1229. jack_client_control_t *control = client->control;
  1230. if (client->engine->real_time) {
  1231. #if JACK_USE_MACH_THREADS
  1232. jack_acquire_real_time_scheduling (client->process_thread,
  1233. client->engine->client_priority);
  1234. #else
  1235. jack_acquire_real_time_scheduling (client->thread,
  1236. client->engine->client_priority);
  1237. #endif
  1238. }
  1239. if (control->freewheel_cb_cbset) {
  1240. client->freewheel_cb (0, client->freewheel_arg);
  1241. }
  1242. }
  1243. static void
  1244. jack_client_thread_suicide (jack_client_t* client)
  1245. {
  1246. if (client->on_info_shutdown) {
  1247. jack_error ("zombified - calling shutdown handler");
  1248. client->on_info_shutdown (JackClientZombie, "Zombified", client->on_info_shutdown_arg);
  1249. } else if (client->on_shutdown) {
  1250. jack_error ("zombified - calling shutdown handler");
  1251. client->on_shutdown (client->on_shutdown_arg);
  1252. } else {
  1253. jack_error ("jack_client_thread zombified - exiting from JACK");
  1254. jack_client_close_aux (client);
  1255. /* Need a fix : possibly make client crash if
  1256. * zombified without shutdown handler
  1257. */
  1258. }
  1259. pthread_exit (0);
  1260. /*NOTREACHED*/
  1261. }
  1262. static int
  1263. jack_client_process_events (jack_client_t* client)
  1264. {
  1265. jack_event_t event;
  1266. char status = 0;
  1267. jack_client_control_t *control = client->control;
  1268. JSList *node;
  1269. jack_port_t* port;
  1270. DEBUG ("process events");
  1271. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1272. DEBUG ("client receives an event, "
  1273. "now reading on event fd");
  1274. /* server has sent us an event. process the
  1275. * event and reply */
  1276. if (read (client->event_fd, &event, sizeof (event))
  1277. != sizeof (event)) {
  1278. jack_error ("cannot read server event (%s)",
  1279. strerror (errno));
  1280. return -1;
  1281. }
  1282. status = 0;
  1283. switch (event.type) {
  1284. case PortRegistered:
  1285. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1286. port = node->data;
  1287. if (port->shared->id == event.x.port_id) { // Found port, update port type
  1288. port->type_info = &client->engine->port_types[port->shared->ptype_id];
  1289. }
  1290. }
  1291. if (control->port_register_cbset) {
  1292. client->port_register
  1293. (event.x.port_id, TRUE,
  1294. client->port_register_arg);
  1295. }
  1296. break;
  1297. case PortUnregistered:
  1298. if (control->port_register_cbset) {
  1299. client->port_register
  1300. (event.x.port_id, FALSE,
  1301. client->port_register_arg);
  1302. }
  1303. break;
  1304. case ClientRegistered:
  1305. if (control->client_register_cbset) {
  1306. client->client_register
  1307. (event.x.name, TRUE,
  1308. client->client_register_arg);
  1309. }
  1310. break;
  1311. case ClientUnregistered:
  1312. if (control->client_register_cbset) {
  1313. client->client_register
  1314. (event.x.name, FALSE,
  1315. client->client_register_arg);
  1316. }
  1317. break;
  1318. case GraphReordered:
  1319. status = jack_handle_reorder (client, &event);
  1320. break;
  1321. case PortConnected:
  1322. case PortDisconnected:
  1323. status = jack_client_handle_port_connection
  1324. (client, &event);
  1325. break;
  1326. case BufferSizeChange:
  1327. jack_client_fix_port_buffers (client);
  1328. if (control->bufsize_cbset) {
  1329. status = client->bufsize
  1330. (control->nframes,
  1331. client->bufsize_arg);
  1332. }
  1333. break;
  1334. case SampleRateChange:
  1335. if (control->srate_cbset) {
  1336. status = client->srate
  1337. (control->nframes,
  1338. client->srate_arg);
  1339. }
  1340. break;
  1341. case XRun:
  1342. if (control->xrun_cbset) {
  1343. status = client->xrun
  1344. (client->xrun_arg);
  1345. }
  1346. break;
  1347. case AttachPortSegment:
  1348. jack_attach_port_segment (client, event.y.ptid);
  1349. break;
  1350. case StartFreewheel:
  1351. jack_start_freewheel (client);
  1352. break;
  1353. case StopFreewheel:
  1354. jack_stop_freewheel (client);
  1355. break;
  1356. case SaveSession:
  1357. status = jack_client_handle_session_callback (client, &event );
  1358. break;
  1359. }
  1360. DEBUG ("client has dealt with the event, writing "
  1361. "response on event fd");
  1362. if (write (client->event_fd, &status, sizeof (status))
  1363. != sizeof (status)) {
  1364. jack_error ("cannot send event response to "
  1365. "engine (%s)", strerror (errno));
  1366. return -1;
  1367. }
  1368. }
  1369. return 0;
  1370. }
  1371. static int
  1372. jack_client_core_wait (jack_client_t* client)
  1373. {
  1374. jack_client_control_t *control = client->control;
  1375. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1376. "event_fd and graph_wait_fd..." :
  1377. "event_fd only");
  1378. while (1) {
  1379. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1380. if (errno == EINTR) {
  1381. continue;
  1382. }
  1383. jack_error ("poll failed in client (%s)",
  1384. strerror (errno));
  1385. return -1;
  1386. }
  1387. pthread_testcancel();
  1388. #ifndef JACK_USE_MACH_THREADS
  1389. /* get an accurate timestamp on waking from poll for a
  1390. * process() cycle.
  1391. */
  1392. if (client->graph_wait_fd >= 0
  1393. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1394. control->awake_at = jack_get_microseconds();
  1395. }
  1396. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1397. client->pollfd[EVENT_POLL_INDEX].revents,
  1398. client->pollfd[WAIT_POLL_INDEX].revents);
  1399. if (client->graph_wait_fd >= 0 &&
  1400. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1401. /* our upstream "wait" connection
  1402. closed, which either means that
  1403. an intermediate client exited, or
  1404. jackd exited, or jackd zombified
  1405. us.
  1406. we can discover the zombification
  1407. via client->control->dead, but
  1408. the other two possibilities are
  1409. impossible to identify just from
  1410. this situation. so we have to
  1411. check what we are connected to,
  1412. and act accordingly.
  1413. */
  1414. if (client->upstream_is_jackd) {
  1415. DEBUG ("WE DIE\n");
  1416. return 0;
  1417. } else {
  1418. DEBUG ("WE PUNT\n");
  1419. /* don't poll on the wait fd
  1420. * again until we get a
  1421. * GraphReordered event.
  1422. */
  1423. client->graph_wait_fd = -1;
  1424. client->pollmax = 1;
  1425. }
  1426. }
  1427. #endif
  1428. if (jack_client_process_events (client)) {
  1429. DEBUG ("event processing failed\n");
  1430. return 0;
  1431. }
  1432. #ifndef JACK_USE_MACH_THREADS
  1433. if (client->graph_wait_fd >= 0 &&
  1434. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1435. DEBUG ("time to run process()\n");
  1436. break;
  1437. }
  1438. #endif
  1439. }
  1440. if (control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1441. DEBUG ("client appears dead or event pollfd has error status\n");
  1442. return -1;
  1443. }
  1444. return 0;
  1445. }
  1446. static int
  1447. jack_wake_next_client (jack_client_t* client)
  1448. {
  1449. struct pollfd pfds[1];
  1450. int pret = 0;
  1451. char c = 0;
  1452. if (write (client->graph_next_fd, &c, sizeof (c))
  1453. != sizeof (c)) {
  1454. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1455. jack_error ("cannot continue execution of the "
  1456. "processing graph (%s)",
  1457. strerror(errno));
  1458. return -1;
  1459. }
  1460. DEBUG ("client sent message to next stage by %" PRIu64 "",
  1461. jack_get_microseconds());
  1462. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1463. /* "upstream client went away? readability is checked in
  1464. * jack_client_core_wait(), but that's almost a whole cycle
  1465. * before we get here.
  1466. */
  1467. if (client->graph_wait_fd >= 0) {
  1468. pfds[0].fd = client->graph_wait_fd;
  1469. pfds[0].events = POLLIN;
  1470. /* 0 timeout, don't actually wait */
  1471. pret = poll(pfds, 1, 0);
  1472. }
  1473. if (pret > 0 && (pfds[0].revents & POLLIN)) {
  1474. if (read (client->graph_wait_fd, &c, sizeof (c))
  1475. != sizeof (c)) {
  1476. jack_error ("cannot complete execution of the "
  1477. "processing graph (%s)", strerror(errno));
  1478. return -1;
  1479. }
  1480. } else {
  1481. DEBUG("cleanup byte from pipe %d not available?\n",
  1482. client->graph_wait_fd);
  1483. }
  1484. return 0;
  1485. }
  1486. static jack_nframes_t
  1487. jack_thread_first_wait (jack_client_t* client)
  1488. {
  1489. if (jack_client_core_wait (client)) {
  1490. return 0;
  1491. }
  1492. return client->control->nframes;
  1493. }
  1494. jack_nframes_t
  1495. jack_thread_wait (jack_client_t* client, int status)
  1496. {
  1497. client->control->last_status = status;
  1498. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1499. /* housekeeping/cleanup after data processing */
  1500. if (status == 0 && client->control->timebase_cb_cbset) {
  1501. jack_call_timebase_master (client);
  1502. }
  1503. /* end preemption checking */
  1504. CHECK_PREEMPTION (client->engine, FALSE);
  1505. client->control->finished_at = jack_get_microseconds();
  1506. /* wake the next client in the chain (could be the server),
  1507. and check if we were killed during the process
  1508. cycle.
  1509. */
  1510. if (jack_wake_next_client (client)) {
  1511. DEBUG("client cannot wake next, or is dead\n");
  1512. return 0;
  1513. }
  1514. if (status || client->control->dead || !client->engine->engine_ok) {
  1515. return 0;
  1516. }
  1517. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1518. if (jack_client_core_wait (client)) {
  1519. return 0;
  1520. }
  1521. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1522. /* Time to do data processing */
  1523. client->control->state = Running;
  1524. /* begin preemption checking */
  1525. CHECK_PREEMPTION (client->engine, TRUE);
  1526. if (client->control->sync_cb_cbset)
  1527. jack_call_sync_client (client);
  1528. return client->control->nframes;
  1529. }
  1530. jack_nframes_t jack_cycle_wait (jack_client_t* client)
  1531. {
  1532. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1533. if (jack_client_core_wait (client)) {
  1534. return 0;
  1535. }
  1536. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1537. /* Time to do data processing */
  1538. client->control->state = Running;
  1539. /* begin preemption checking */
  1540. CHECK_PREEMPTION (client->engine, TRUE);
  1541. if (client->control->sync_cb_cbset)
  1542. jack_call_sync_client (client);
  1543. return client->control->nframes;
  1544. }
  1545. void jack_cycle_signal(jack_client_t* client, int status)
  1546. {
  1547. client->control->last_status = status;
  1548. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1549. /* housekeeping/cleanup after data processing */
  1550. if (status == 0 && client->control->timebase_cb_cbset) {
  1551. jack_call_timebase_master (client);
  1552. }
  1553. /* end preemption checking */
  1554. CHECK_PREEMPTION (client->engine, FALSE);
  1555. client->control->finished_at = jack_get_microseconds();
  1556. /* wake the next client in the chain (could be the server),
  1557. and check if we were killed during the process
  1558. cycle.
  1559. */
  1560. if (jack_wake_next_client (client)) {
  1561. DEBUG("client cannot wake next, or is dead\n");
  1562. jack_client_thread_suicide (client);
  1563. /*NOTREACHED*/
  1564. }
  1565. if (status || client->control->dead || !client->engine->engine_ok) {
  1566. jack_client_thread_suicide (client);
  1567. /*NOTREACHED*/
  1568. }
  1569. }
  1570. static void
  1571. jack_client_thread_aux (void *arg)
  1572. {
  1573. jack_client_t *client = (jack_client_t *) arg;
  1574. jack_client_control_t *control = client->control;
  1575. pthread_mutex_lock (&client_lock);
  1576. client->thread_ok = TRUE;
  1577. client->thread_id = pthread_self();
  1578. pthread_cond_signal (&client_ready);
  1579. pthread_mutex_unlock (&client_lock);
  1580. control->pid = getpid();
  1581. control->pgrp = getpgrp();
  1582. DEBUG ("client thread is now running");
  1583. if (control->thread_init_cbset) {
  1584. DEBUG ("calling client thread init callback");
  1585. client->thread_init (client->thread_init_arg);
  1586. }
  1587. /* wait for first wakeup from server */
  1588. if (jack_thread_first_wait (client) == control->nframes) {
  1589. /* now run till we're done */
  1590. if (control->process_cbset) {
  1591. /* run process callback, then wait... ad-infinitum */
  1592. while (1) {
  1593. DEBUG("client calls process()");
  1594. int status = (client->process (control->nframes,
  1595. client->process_arg) ==
  1596. control->nframes);
  1597. control->state = Finished;
  1598. DEBUG("client leaves process(), re-enters wait");
  1599. if (!jack_thread_wait (client, status)) {
  1600. break;
  1601. }
  1602. DEBUG("client done with wait");
  1603. }
  1604. } else {
  1605. /* no process handling but still need to process events */
  1606. while (jack_thread_wait (client, 0) == control->nframes)
  1607. ;
  1608. }
  1609. }
  1610. jack_client_thread_suicide (client);
  1611. }
  1612. static void*
  1613. jack_client_thread (void *arg)
  1614. {
  1615. jack_client_t *client = (jack_client_t *) arg;
  1616. jack_client_control_t *control = client->control;
  1617. if (client->control->thread_cb_cbset) {
  1618. pthread_mutex_lock (&client_lock);
  1619. client->thread_ok = TRUE;
  1620. client->thread_id = pthread_self();
  1621. pthread_cond_signal (&client_ready);
  1622. pthread_mutex_unlock (&client_lock);
  1623. control->pid = getpid();
  1624. control->pgrp = getpgrp();
  1625. client->thread_cb(client->thread_cb_arg);
  1626. jack_client_thread_suicide(client);
  1627. } else {
  1628. jack_client_thread_aux(arg);
  1629. }
  1630. /*NOTREACHED*/
  1631. return (void *) 0;
  1632. }
  1633. #ifdef JACK_USE_MACH_THREADS
  1634. /* real-time thread : separated from the normal client thread, it will
  1635. * communicate with the server using fast mach RPC mechanism */
  1636. static void *
  1637. jack_client_process_thread (void *arg)
  1638. {
  1639. jack_client_t *client = (jack_client_t *) arg;
  1640. jack_client_control_t *control = client->control;
  1641. int err = 0;
  1642. if (client->control->thread_init_cbset) {
  1643. /* this means that the init callback will be called twice -taybin*/
  1644. DEBUG ("calling client thread init callback");
  1645. client->thread_init (client->thread_init_arg);
  1646. }
  1647. client->control->pid = getpid();
  1648. DEBUG ("client process thread is now running");
  1649. client->rt_thread_ok = TRUE;
  1650. while (err == 0) {
  1651. if (jack_client_suspend(client) < 0) {
  1652. jack_error ("jack_client_process_thread :resume error");
  1653. goto zombie;
  1654. }
  1655. control->awake_at = jack_get_microseconds();
  1656. DEBUG ("client resumed");
  1657. control->state = Running;
  1658. if (control->sync_cb_cbset)
  1659. jack_call_sync_client (client);
  1660. if (control->process_cbset) {
  1661. if (client->process (control->nframes,
  1662. client->process_arg) == 0) {
  1663. control->state = Finished;
  1664. }
  1665. } else {
  1666. control->state = Finished;
  1667. }
  1668. if (control->timebase_cb_cbset)
  1669. jack_call_timebase_master (client);
  1670. control->finished_at = jack_get_microseconds();
  1671. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1672. control->finished_at,
  1673. ((float)(control->finished_at - control->awake_at)));
  1674. /* check if we were killed during the process cycle
  1675. * (or whatever) */
  1676. if (client->control->dead) {
  1677. jack_error ("jack_client_process_thread: "
  1678. "client->control->dead");
  1679. goto zombie;
  1680. }
  1681. DEBUG("process cycle fully complete\n");
  1682. }
  1683. return (void *) ((intptr_t)err);
  1684. zombie:
  1685. jack_error ("jack_client_process_thread : zombified");
  1686. client->rt_thread_ok = FALSE;
  1687. if (client->on_info_shutdown) {
  1688. jack_error ("zombified - calling shutdown handler");
  1689. client->on_info_shutdown (JackClientZombie, "Zombified", client->on_info_shutdown_arg);
  1690. } else if (client->on_shutdown) {
  1691. jack_error ("zombified - calling shutdown handler");
  1692. client->on_shutdown (client->on_shutdown_arg);
  1693. } else {
  1694. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1695. /* Need a fix : possibly make client crash if
  1696. * zombified without shutdown handler */
  1697. jack_client_close_aux (client);
  1698. }
  1699. pthread_exit (0);
  1700. /*NOTREACHED*/
  1701. return 0;
  1702. }
  1703. #endif /* JACK_USE_MACH_THREADS */
  1704. static int
  1705. jack_start_thread (jack_client_t *client)
  1706. {
  1707. if (client->engine->real_time) {
  1708. #ifdef USE_MLOCK
  1709. if (client->engine->do_mlock
  1710. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1711. jack_error ("cannot lock down memory for RT thread "
  1712. "(%s)", strerror (errno));
  1713. #ifdef ENSURE_MLOCK
  1714. return -1;
  1715. #endif /* ENSURE_MLOCK */
  1716. }
  1717. if (client->engine->do_munlock) {
  1718. cleanup_mlock ();
  1719. }
  1720. #endif /* USE_MLOCK */
  1721. }
  1722. #ifdef JACK_USE_MACH_THREADS
  1723. /* Stephane Letz : letz@grame.fr
  1724. On MacOSX, the normal thread does not need to be real-time.
  1725. */
  1726. if (jack_client_create_thread (client,
  1727. &client->thread,
  1728. client->engine->client_priority,
  1729. FALSE,
  1730. jack_client_thread, client)) {
  1731. return -1;
  1732. }
  1733. #else
  1734. if (jack_client_create_thread (client,
  1735. &client->thread,
  1736. client->engine->client_priority,
  1737. client->engine->real_time,
  1738. jack_client_thread, client)) {
  1739. return -1;
  1740. }
  1741. #endif
  1742. #ifdef JACK_USE_MACH_THREADS
  1743. /* a secondary thread that runs the process callback and uses
  1744. ultra-fast Mach primitives for inter-thread signalling.
  1745. XXX in a properly structured JACK, there would be no
  1746. need for this, because we would have client wake up
  1747. methods that encapsulated the underlying mechanism
  1748. used.
  1749. */
  1750. if (jack_client_create_thread(client,
  1751. &client->process_thread,
  1752. client->engine->client_priority,
  1753. client->engine->real_time,
  1754. jack_client_process_thread, client)) {
  1755. return -1;
  1756. }
  1757. #endif /* JACK_USE_MACH_THREADS */
  1758. return 0;
  1759. }
  1760. int
  1761. jack_activate (jack_client_t *client)
  1762. {
  1763. jack_request_t req;
  1764. /* we need to scribble on our stack to ensure that its memory
  1765. * pages are actually mapped (more important for mlockall(2)
  1766. * usage in jack_start_thread())
  1767. */
  1768. //char buf[JACK_THREAD_STACK_TOUCH];
  1769. int i;
  1770. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1771. //buf[i] = (char) (i & 0xff);
  1772. }
  1773. if (client->control->type == ClientInternal ||
  1774. client->control->type == ClientDriver) {
  1775. goto startit;
  1776. }
  1777. /* get the pid of the client process to pass it to engine */
  1778. client->control->pid = getpid ();
  1779. #ifdef USE_CAPABILITIES
  1780. if (client->engine->has_capabilities != 0 &&
  1781. client->control->pid != 0 && client->engine->real_time != 0) {
  1782. /* we need to ask the engine for realtime capabilities
  1783. before trying to start the realtime thread
  1784. */
  1785. req.type = SetClientCapabilities;
  1786. req.x.client_id = client->control->id;
  1787. req.x.cap_pid = client->control->pid;
  1788. jack_client_deliver_request (client, &req);
  1789. if (req.status) {
  1790. /* what to do? engine is running realtime, it
  1791. is using capabilities and has them
  1792. (otherwise we would not get an error
  1793. return) but for some reason it could not
  1794. give the client the required capabilities.
  1795. For now, leave the client so that it
  1796. still runs, albeit non-realtime.
  1797. */
  1798. jack_error ("could not receive realtime capabilities, "
  1799. "client will run non-realtime");
  1800. }
  1801. }
  1802. #endif /* USE_CAPABILITIES */
  1803. if (client->first_active) {
  1804. pthread_mutex_init (&client_lock, NULL);
  1805. pthread_cond_init (&client_ready, NULL);
  1806. pthread_mutex_lock (&client_lock);
  1807. if (jack_start_thread (client)) {
  1808. pthread_mutex_unlock (&client_lock);
  1809. return -1;
  1810. }
  1811. pthread_cond_wait (&client_ready, &client_lock);
  1812. pthread_mutex_unlock (&client_lock);
  1813. if (!client->thread_ok) {
  1814. jack_error ("could not start client thread");
  1815. return -1;
  1816. }
  1817. client->first_active = FALSE;
  1818. }
  1819. startit:
  1820. req.type = ActivateClient;
  1821. req.x.client_id = client->control->id;
  1822. return jack_client_deliver_request (client, &req);
  1823. }
  1824. static int
  1825. jack_deactivate_aux (jack_client_t *client)
  1826. {
  1827. jack_request_t req;
  1828. int rc = ESRCH; /* already shut down */
  1829. if (client && client->control) { /* not shut down? */
  1830. rc = 0;
  1831. if (client->control->active) { /* still active? */
  1832. req.type = DeactivateClient;
  1833. req.x.client_id = client->control->id;
  1834. rc = jack_client_deliver_request (client, &req);
  1835. }
  1836. }
  1837. return rc;
  1838. }
  1839. int
  1840. jack_deactivate (jack_client_t *client)
  1841. {
  1842. return jack_deactivate_aux(client);
  1843. }
  1844. static int
  1845. jack_client_close_aux (jack_client_t *client)
  1846. {
  1847. JSList *node;
  1848. void *status;
  1849. int rc;
  1850. rc = jack_deactivate_aux (client);
  1851. if (rc == ESRCH) { /* already shut down? */
  1852. return rc;
  1853. }
  1854. if (client->control->type == ClientExternal) {
  1855. #if JACK_USE_MACH_THREADS
  1856. if (client->rt_thread_ok) {
  1857. // MacOSX pthread_cancel not implemented in
  1858. // Darwin 5.5, 6.4
  1859. mach_port_t machThread =
  1860. pthread_mach_thread_np (client->process_thread);
  1861. thread_terminate (machThread);
  1862. }
  1863. #endif
  1864. /* stop the thread that communicates with the jack
  1865. * server, only if it was actually running
  1866. */
  1867. if (client->thread_ok){
  1868. pthread_cancel (client->thread);
  1869. pthread_join (client->thread, &status);
  1870. }
  1871. if (client->control) {
  1872. jack_release_shm (&client->control_shm);
  1873. client->control = NULL;
  1874. }
  1875. if (client->engine) {
  1876. jack_release_shm (&client->engine_shm);
  1877. client->engine = NULL;
  1878. }
  1879. if (client->port_segment) {
  1880. jack_port_type_id_t ptid;
  1881. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1882. jack_release_shm (&client->port_segment[ptid]);
  1883. }
  1884. free (client->port_segment);
  1885. client->port_segment = NULL;
  1886. }
  1887. #ifndef JACK_USE_MACH_THREADS
  1888. if (client->graph_wait_fd >= 0) {
  1889. close (client->graph_wait_fd);
  1890. }
  1891. if (client->graph_next_fd >= 0) {
  1892. close (client->graph_next_fd);
  1893. }
  1894. #endif
  1895. close (client->event_fd);
  1896. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1897. jack_error ("could not shutdown client request socket");
  1898. }
  1899. close (client->request_fd);
  1900. }
  1901. for (node = client->ports; node; node = jack_slist_next (node)) {
  1902. free (node->data);
  1903. }
  1904. jack_slist_free (client->ports);
  1905. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1906. free (node->data);
  1907. }
  1908. jack_slist_free (client->ports_ext);
  1909. jack_client_free (client);
  1910. jack_messagebuffer_exit ();
  1911. return rc;
  1912. }
  1913. int
  1914. jack_client_close (jack_client_t *client)
  1915. {
  1916. return jack_client_close_aux(client);
  1917. }
  1918. int
  1919. jack_is_realtime (jack_client_t *client)
  1920. {
  1921. return client->engine->real_time;
  1922. }
  1923. jack_nframes_t
  1924. jack_get_buffer_size (jack_client_t *client)
  1925. {
  1926. return client->engine->buffer_size;
  1927. }
  1928. int
  1929. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1930. {
  1931. #ifdef DO_BUFFER_RESIZE
  1932. jack_request_t req;
  1933. req.type = SetBufferSize;
  1934. req.x.nframes = nframes;
  1935. return jack_client_deliver_request (client, &req);
  1936. #else
  1937. return ENOSYS;
  1938. #endif /* DO_BUFFER_RESIZE */
  1939. }
  1940. int
  1941. jack_connect (jack_client_t *client, const char *source_port,
  1942. const char *destination_port)
  1943. {
  1944. jack_request_t req;
  1945. req.type = ConnectPorts;
  1946. snprintf (req.x.connect.source_port,
  1947. sizeof (req.x.connect.source_port), "%s", source_port);
  1948. snprintf (req.x.connect.destination_port,
  1949. sizeof (req.x.connect.destination_port),
  1950. "%s", destination_port);
  1951. return jack_client_deliver_request (client, &req);
  1952. }
  1953. int
  1954. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1955. {
  1956. jack_request_t req;
  1957. pthread_mutex_lock (&port->connection_lock);
  1958. if (port->connections == NULL) {
  1959. pthread_mutex_unlock (&port->connection_lock);
  1960. return 0;
  1961. }
  1962. pthread_mutex_unlock (&port->connection_lock);
  1963. req.type = DisconnectPort;
  1964. req.x.port_info.port_id = port->shared->id;
  1965. return jack_client_deliver_request (client, &req);
  1966. }
  1967. int
  1968. jack_disconnect (jack_client_t *client, const char *source_port,
  1969. const char *destination_port)
  1970. {
  1971. jack_request_t req;
  1972. req.type = DisconnectPorts;
  1973. snprintf (req.x.connect.source_port,
  1974. sizeof (req.x.connect.source_port), "%s", source_port);
  1975. snprintf (req.x.connect.destination_port,
  1976. sizeof (req.x.connect.destination_port),
  1977. "%s", destination_port);
  1978. return jack_client_deliver_request (client, &req);
  1979. }
  1980. void
  1981. jack_set_error_function (void (*func) (const char *))
  1982. {
  1983. jack_error_callback = func;
  1984. }
  1985. void
  1986. jack_set_info_function (void (*func) (const char *))
  1987. {
  1988. jack_info_callback = func;
  1989. }
  1990. int
  1991. jack_set_graph_order_callback (jack_client_t *client,
  1992. JackGraphOrderCallback callback, void *arg)
  1993. {
  1994. if (client->control->active) {
  1995. jack_error ("You cannot set callbacks on an active client.");
  1996. return -1;
  1997. }
  1998. client->graph_order = callback;
  1999. client->graph_order_arg = arg;
  2000. client->control->graph_order_cbset = (callback != NULL);
  2001. return 0;
  2002. }
  2003. int jack_set_xrun_callback (jack_client_t *client,
  2004. JackXRunCallback callback, void *arg)
  2005. {
  2006. if (client->control->active) {
  2007. jack_error ("You cannot set callbacks on an active client.");
  2008. return -1;
  2009. }
  2010. client->xrun = callback;
  2011. client->xrun_arg = arg;
  2012. client->control->xrun_cbset = (callback != NULL);
  2013. return 0;
  2014. }
  2015. int
  2016. jack_set_process_callback (jack_client_t *client,
  2017. JackProcessCallback callback, void *arg)
  2018. {
  2019. if (client->control->active) {
  2020. jack_error ("You cannot set callbacks on an active client.");
  2021. return -1;
  2022. }
  2023. if (client->control->thread_cb_cbset) {
  2024. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  2025. return -1;
  2026. }
  2027. client->process_arg = arg;
  2028. client->process = callback;
  2029. client->control->process_cbset = (callback != NULL);
  2030. return 0;
  2031. }
  2032. int
  2033. jack_set_thread_init_callback (jack_client_t *client,
  2034. JackThreadInitCallback callback, void *arg)
  2035. {
  2036. if (client->control->active) {
  2037. jack_error ("You cannot set callbacks on an active client.");
  2038. return -1;
  2039. }
  2040. client->thread_init_arg = arg;
  2041. client->thread_init = callback;
  2042. client->control->thread_init_cbset = (callback != NULL);
  2043. /* make sure that the message buffer thread is initialized too */
  2044. jack_messagebuffer_thread_init (callback, arg);
  2045. return 0;
  2046. }
  2047. int
  2048. jack_set_freewheel_callback (jack_client_t *client,
  2049. JackFreewheelCallback callback, void *arg)
  2050. {
  2051. if (client->control->active) {
  2052. jack_error ("You cannot set callbacks on an active client.");
  2053. return -1;
  2054. }
  2055. client->freewheel_arg = arg;
  2056. client->freewheel_cb = callback;
  2057. client->control->freewheel_cb_cbset = (callback != NULL);
  2058. return 0;
  2059. }
  2060. int
  2061. jack_set_buffer_size_callback (jack_client_t *client,
  2062. JackBufferSizeCallback callback, void *arg)
  2063. {
  2064. client->bufsize_arg = arg;
  2065. client->bufsize = callback;
  2066. client->control->bufsize_cbset = (callback != NULL);
  2067. return 0;
  2068. }
  2069. int
  2070. jack_set_port_registration_callback(jack_client_t *client,
  2071. JackPortRegistrationCallback callback,
  2072. void *arg)
  2073. {
  2074. if (client->control->active) {
  2075. jack_error ("You cannot set callbacks on an active client.");
  2076. return -1;
  2077. }
  2078. client->port_register_arg = arg;
  2079. client->port_register = callback;
  2080. client->control->port_register_cbset = (callback != NULL);
  2081. return 0;
  2082. }
  2083. int
  2084. jack_set_port_connect_callback(jack_client_t *client,
  2085. JackPortConnectCallback callback,
  2086. void *arg)
  2087. {
  2088. if (client->control->active) {
  2089. jack_error ("You cannot set callbacks on an active client.");
  2090. return -1;
  2091. }
  2092. client->port_connect_arg = arg;
  2093. client->port_connect = callback;
  2094. client->control->port_connect_cbset = (callback != NULL);
  2095. return 0;
  2096. }
  2097. int
  2098. jack_set_client_registration_callback(jack_client_t *client,
  2099. JackClientRegistrationCallback callback,
  2100. void *arg)
  2101. {
  2102. if (client->control->active) {
  2103. jack_error ("You cannot set callbacks on an active client.");
  2104. return -1;
  2105. }
  2106. client->client_register_arg = arg;
  2107. client->client_register = callback;
  2108. client->control->client_register_cbset = (callback != NULL);
  2109. return 0;
  2110. }
  2111. int
  2112. jack_set_process_thread(jack_client_t* client, JackThreadCallback callback, void *arg)
  2113. {
  2114. if (client->control->active) {
  2115. jack_error ("You cannot set callbacks on an active client.");
  2116. return -1;
  2117. }
  2118. if (client->control->process_cbset) {
  2119. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  2120. return -1;
  2121. }
  2122. client->thread_cb_arg = arg;
  2123. client->thread_cb = callback;
  2124. client->control->thread_cb_cbset = (callback != NULL);
  2125. return 0;
  2126. }
  2127. int
  2128. jack_set_session_callback(jack_client_t* client, JackSessionCallback callback, void *arg)
  2129. {
  2130. if (client->control->active) {
  2131. jack_error ("You cannot set callbacks on an active client.");
  2132. return -1;
  2133. }
  2134. client->session_cb_arg = arg;
  2135. client->session_cb = callback;
  2136. client->control->session_cbset = (callback != NULL);
  2137. return 0;
  2138. }
  2139. int
  2140. jack_get_process_done_fd (jack_client_t *client)
  2141. {
  2142. return client->graph_next_fd;
  2143. }
  2144. void
  2145. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  2146. {
  2147. client->on_shutdown = function;
  2148. client->on_shutdown_arg = arg;
  2149. }
  2150. void
  2151. jack_on_info_shutdown (jack_client_t *client, void (*function)(jack_status_t, const char*, void *arg), void *arg)
  2152. {
  2153. client->on_info_shutdown = function;
  2154. client->on_info_shutdown_arg = arg;
  2155. }
  2156. char *
  2157. jack_get_client_name_by_uuid( jack_client_t *client, const char *uuid )
  2158. {
  2159. jack_request_t request;
  2160. char *end_ptr;
  2161. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2162. if( *end_ptr != '\0' )
  2163. return NULL;
  2164. request.type = GetClientByUUID;
  2165. request.x.client_id = uuid_int;
  2166. if( jack_client_deliver_request( client, &request ) )
  2167. return NULL;
  2168. return strdup( request.x.port_info.name );
  2169. }
  2170. int
  2171. jack_reserve_client_name( jack_client_t *client, const char *name, const char *uuid )
  2172. {
  2173. jack_request_t request;
  2174. char *end_ptr;
  2175. jack_client_id_t uuid_int = strtol( uuid, &end_ptr, 10 );
  2176. if( *end_ptr != '\0' )
  2177. return -1;
  2178. request.type = ReserveName;
  2179. snprintf( request.x.reservename.name, sizeof( request.x.reservename.name ),
  2180. "%s", name );
  2181. request.x.reservename.uuid = uuid_int;
  2182. return jack_client_deliver_request( client, &request );
  2183. }
  2184. int
  2185. jack_rename_client( jack_client_t *client, const char *oldname, const char *newname )
  2186. {
  2187. jack_request_t request;
  2188. request.type = RenameClient;
  2189. snprintf( request.x.clientrename.oldname, sizeof( request.x.clientrename.oldname ),
  2190. "%s", oldname );
  2191. snprintf( request.x.clientrename.newname, sizeof( request.x.clientrename.newname ),
  2192. "%s", newname );
  2193. return jack_client_deliver_request( client, &request );
  2194. }
  2195. const char **
  2196. jack_get_ports (jack_client_t *client,
  2197. const char *port_name_pattern,
  2198. const char *type_name_pattern,
  2199. unsigned long flags)
  2200. {
  2201. jack_control_t *engine;
  2202. const char **matching_ports;
  2203. unsigned long match_cnt;
  2204. jack_port_shared_t *psp;
  2205. unsigned long i;
  2206. regex_t port_regex;
  2207. regex_t type_regex;
  2208. int matching;
  2209. engine = client->engine;
  2210. if (port_name_pattern && port_name_pattern[0]) {
  2211. regcomp (&port_regex, port_name_pattern,
  2212. REG_EXTENDED|REG_NOSUB);
  2213. }
  2214. if (type_name_pattern && type_name_pattern[0]) {
  2215. regcomp (&type_regex, type_name_pattern,
  2216. REG_EXTENDED|REG_NOSUB);
  2217. }
  2218. psp = engine->ports;
  2219. match_cnt = 0;
  2220. if ((matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max)) == NULL) {
  2221. return NULL;
  2222. }
  2223. for (i = 0; i < engine->port_max; i++) {
  2224. matching = 1;
  2225. if (!psp[i].in_use) {
  2226. continue;
  2227. }
  2228. if (flags) {
  2229. if ((psp[i].flags & flags) != flags) {
  2230. matching = 0;
  2231. }
  2232. }
  2233. if (matching && port_name_pattern && port_name_pattern[0]) {
  2234. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  2235. matching = 0;
  2236. }
  2237. }
  2238. if (matching && type_name_pattern && type_name_pattern[0]) {
  2239. jack_port_type_id_t ptid = psp[i].ptype_id;
  2240. if (regexec (&type_regex,
  2241. engine->port_types[ptid].type_name,
  2242. 0, NULL, 0)) {
  2243. matching = 0;
  2244. }
  2245. }
  2246. if (matching) {
  2247. matching_ports[match_cnt++] = psp[i].name;
  2248. }
  2249. }
  2250. if (port_name_pattern && port_name_pattern[0]) {
  2251. regfree (&port_regex);
  2252. }
  2253. if (type_name_pattern && type_name_pattern[0]) {
  2254. regfree (&type_regex);
  2255. }
  2256. matching_ports[match_cnt] = 0;
  2257. if (match_cnt == 0) {
  2258. free (matching_ports);
  2259. matching_ports = 0;
  2260. }
  2261. return matching_ports;
  2262. }
  2263. float
  2264. jack_cpu_load (jack_client_t *client)
  2265. {
  2266. return client->engine->cpu_load;
  2267. }
  2268. float
  2269. jack_get_xrun_delayed_usecs (jack_client_t *client)
  2270. {
  2271. return client->engine->xrun_delayed_usecs;
  2272. }
  2273. float
  2274. jack_get_max_delayed_usecs (jack_client_t *client)
  2275. {
  2276. return client->engine->max_delayed_usecs;
  2277. }
  2278. void
  2279. jack_reset_max_delayed_usecs (jack_client_t *client)
  2280. {
  2281. client->engine->max_delayed_usecs = 0.0f;
  2282. }
  2283. pthread_t
  2284. jack_client_thread_id (jack_client_t *client)
  2285. {
  2286. return client->thread_id;
  2287. }
  2288. int
  2289. jack_client_name_size(void)
  2290. {
  2291. return JACK_CLIENT_NAME_SIZE;
  2292. }
  2293. int
  2294. jack_port_name_size(void)
  2295. {
  2296. return JACK_PORT_NAME_SIZE;
  2297. }
  2298. int
  2299. jack_port_type_size(void)
  2300. {
  2301. return JACK_PORT_TYPE_SIZE;
  2302. }
  2303. void
  2304. jack_free (void* ptr)
  2305. {
  2306. free (ptr);
  2307. }