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.

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