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.

3024 lines
71KB

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