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.

2860 lines
66KB

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