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.

3190 lines
73KB

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