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.

3007 lines
70KB

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