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.

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