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.

3122 lines
75KB

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