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.

3072 lines
74KB

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