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.

1958 lines
45KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #if defined(__APPLE__) && defined(__POWERPC__)
  18. #include "pThreadUtilities.h"
  19. #include "ipc.h"
  20. #include "fakepoll.h"
  21. #else
  22. #include <sys/poll.h>
  23. #endif
  24. #include <sys/socket.h>
  25. #include <sys/un.h>
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/ipc.h>
  31. #include <sys/mman.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <regex.h>
  36. #include <signal.h>
  37. #include <config.h>
  38. #include <jack/jack.h>
  39. #include <jack/internal.h>
  40. #include <jack/engine.h>
  41. #include <jack/pool.h>
  42. #include <jack/time.h>
  43. #include <jack/jslist.h>
  44. #include <jack/version.h>
  45. #include <jack/shm.h>
  46. #include "local.h"
  47. #ifdef WITH_TIMESTAMPS
  48. #include <jack/timestamps.h>
  49. #endif /* WITH_TIMESTAMPS */
  50. #ifdef DEFAULT_TMP_DIR
  51. char *jack_server_dir = DEFAULT_TMP_DIR;
  52. #else
  53. char *jack_server_dir = "/tmp";
  54. #endif
  55. void
  56. jack_set_server_dir (const char *path)
  57. {
  58. jack_error ("jack_set_server_dir() is deprecated.\n"
  59. "Please contact the program's author");
  60. jack_server_dir = strdup (path);
  61. }
  62. static pthread_mutex_t client_lock;
  63. static pthread_cond_t client_ready;
  64. void *jack_zero_filled_buffer = NULL;
  65. #define event_fd pollfd[0].fd
  66. #define graph_wait_fd pollfd[1].fd
  67. typedef struct {
  68. int status;
  69. struct _jack_client *client;
  70. const char *client_name;
  71. } client_info;
  72. void
  73. jack_error (const char *fmt, ...)
  74. {
  75. va_list ap;
  76. char buffer[300];
  77. va_start (ap, fmt);
  78. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  79. jack_error_callback (buffer);
  80. va_end (ap);
  81. }
  82. void
  83. default_jack_error_callback (const char *desc)
  84. {
  85. fprintf(stderr, "%s\n", desc);
  86. }
  87. void
  88. silent_jack_error_callback (const char *desc)
  89. {
  90. }
  91. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  92. static int
  93. oop_client_deliver_request (void *ptr, jack_request_t *req)
  94. {
  95. int wok, rok;
  96. jack_client_t *client = (jack_client_t*) ptr;
  97. wok = (write (client->request_fd, req, sizeof (*req))
  98. == sizeof (*req));
  99. rok = (read (client->request_fd, req, sizeof (*req))
  100. == sizeof (*req));
  101. if (wok && rok) /* everything OK? */
  102. return req->status;
  103. req->status = -1; /* request failed */
  104. /* check for server shutdown */
  105. if (client->engine->engine_ok == 0)
  106. return req->status;
  107. /* otherwise report errors */
  108. if (!wok)
  109. jack_error ("cannot send request type %d to server",
  110. req->type);
  111. if (!rok)
  112. jack_error ("cannot read result for request type %d from"
  113. " server (%s)", req->type, strerror (errno));
  114. return req->status;
  115. }
  116. int
  117. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  118. {
  119. /* indirect through the function pointer that was set
  120. either by jack_client_new() (external) or handle_new_client()
  121. in the server.
  122. */
  123. return client->control->deliver_request (client->control->deliver_arg, req);
  124. }
  125. jack_client_t *
  126. jack_client_alloc ()
  127. {
  128. jack_client_t *client;
  129. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  130. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  131. client->pollmax = 2;
  132. client->request_fd = -1;
  133. client->event_fd = -1;
  134. client->graph_wait_fd = -1;
  135. client->graph_next_fd = -1;
  136. client->ports = NULL;
  137. client->engine = NULL;
  138. client->control = NULL;
  139. client->thread_ok = FALSE;
  140. client->first_active = TRUE;
  141. client->on_shutdown = NULL;
  142. client->n_port_types = 0;
  143. client->port_segment = NULL;
  144. return client;
  145. }
  146. /*
  147. * Build the jack_client_t structure for an internal client.
  148. */
  149. jack_client_t *
  150. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  151. {
  152. jack_client_t* client;
  153. client = jack_client_alloc ();
  154. client->control = cc;
  155. client->engine = engine->control;
  156. client->n_port_types = client->engine->n_port_types;
  157. client->port_segment = &engine->port_segment[0];
  158. return client;
  159. }
  160. static void
  161. jack_client_free (jack_client_t *client)
  162. {
  163. if (client->pollfd) {
  164. free (client->pollfd);
  165. }
  166. free (client);
  167. }
  168. void
  169. jack_client_invalidate_port_buffers (jack_client_t *client)
  170. {
  171. JSList *node;
  172. jack_port_t *port;
  173. /* This releases all local memory owned by input ports
  174. and sets the buffer pointer to NULL. This will cause
  175. jack_port_get_buffer() to reallocate space for the
  176. buffer on the next call (if there is one).
  177. */
  178. for (node = client->ports; node; node = jack_slist_next (node)) {
  179. port = (jack_port_t *) node->data;
  180. if (port->shared->flags & JackPortIsInput) {
  181. if (port->mix_buffer) {
  182. jack_pool_release (port->mix_buffer);
  183. port->mix_buffer = NULL;
  184. }
  185. }
  186. }
  187. }
  188. int
  189. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  190. {
  191. jack_port_t *control_port;
  192. jack_port_t *other;
  193. JSList *node;
  194. switch (event->type) {
  195. case PortConnected:
  196. other = jack_port_new (client, event->y.other_id, client->engine);
  197. control_port = jack_port_by_id (client, event->x.self_id);
  198. pthread_mutex_lock (&control_port->connection_lock);
  199. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  200. pthread_mutex_unlock (&control_port->connection_lock);
  201. break;
  202. case PortDisconnected:
  203. control_port = jack_port_by_id (client, event->x.self_id);
  204. pthread_mutex_lock (&control_port->connection_lock);
  205. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  206. other = (jack_port_t *) node->data;
  207. if (other->shared->id == event->y.other_id) {
  208. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  209. jack_slist_free_1 (node);
  210. free (other);
  211. break;
  212. }
  213. }
  214. pthread_mutex_unlock (&control_port->connection_lock);
  215. break;
  216. default:
  217. /* impossible */
  218. break;
  219. }
  220. return 0;
  221. }
  222. static int
  223. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  224. {
  225. char path[PATH_MAX+1];
  226. if (client->graph_wait_fd >= 0) {
  227. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  228. close (client->graph_wait_fd);
  229. client->graph_wait_fd = -1;
  230. }
  231. if (client->graph_next_fd >= 0) {
  232. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  233. close (client->graph_next_fd);
  234. client->graph_next_fd = -1;
  235. }
  236. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  237. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  238. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  239. return -1;
  240. }
  241. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  242. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  243. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  244. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  245. return -1;
  246. }
  247. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  248. /* If the client registered its own callback for graph order events,
  249. execute it now.
  250. */
  251. if (client->control->graph_order) {
  252. client->control->graph_order (client->control->graph_order_arg);
  253. }
  254. return 0;
  255. }
  256. static int
  257. server_connect (int which)
  258. {
  259. int fd;
  260. struct sockaddr_un addr;
  261. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  262. jack_error ("cannot create client socket (%s)",
  263. strerror (errno));
  264. return -1;
  265. }
  266. addr.sun_family = AF_UNIX;
  267. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  268. jack_server_dir, which);
  269. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  270. close (fd);
  271. return -1;
  272. }
  273. return fd;
  274. }
  275. static int
  276. server_event_connect (jack_client_t *client)
  277. {
  278. int fd;
  279. struct sockaddr_un addr;
  280. jack_client_connect_ack_request_t req;
  281. jack_client_connect_ack_result_t res;
  282. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  283. jack_error ("cannot create client event socket (%s)",
  284. strerror (errno));
  285. return -1;
  286. }
  287. addr.sun_family = AF_UNIX;
  288. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  289. jack_server_dir);
  290. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  291. jack_error ("cannot connect to jack server for events",
  292. strerror (errno));
  293. close (fd);
  294. return -1;
  295. }
  296. req.client_id = client->control->id;
  297. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  298. jack_error ("cannot write event connect request to server (%s)",
  299. strerror (errno));
  300. close (fd);
  301. return -1;
  302. }
  303. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  304. jack_error ("cannot read event connect result from server (%s)",
  305. strerror (errno));
  306. close (fd);
  307. return -1;
  308. }
  309. if (res.status != 0) {
  310. jack_error ("cannot connect to server for event stream (%s)",
  311. strerror (errno));
  312. close (fd);
  313. return -1;
  314. }
  315. return fd;
  316. }
  317. static void
  318. _start_server (void)
  319. {
  320. FILE* fp = 0;
  321. char filename[255];
  322. char arguments[255];
  323. char buffer[255];
  324. char* command = 0;
  325. size_t pos = 0;
  326. size_t result = 0;
  327. char** argv = 0;
  328. int i = 0;
  329. int good = 0;
  330. int ret;
  331. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  332. fp = fopen(filename, "r");
  333. if (!fp) {
  334. fp = fopen("/etc/jackd.conf", "r");
  335. }
  336. if (fp) {
  337. arguments[0] = '\0';
  338. ret = fscanf(fp, "%s", buffer);
  339. while(ret != 0 && ret != EOF) {
  340. strcat(arguments, buffer);
  341. strcat(arguments, " ");
  342. ret = fscanf(fp, "%s", buffer);
  343. }
  344. if (strlen(arguments) > 0) {
  345. good = 1;
  346. }
  347. }
  348. if (!good) {
  349. #if defined(__APPLE__) && defined(__POWERPC__)
  350. command = JACK_LOCATION "/jackd";
  351. strncpy(arguments, JACK_LOCATION "/jackd -T -d portaudio -p 512", 255);
  352. #elif defined(USE_CAPABILITIES)
  353. command = JACK_LOCATION "/jackstart";
  354. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d alsa -d hw:0 -p 512", 255);
  355. #else
  356. command = JACK_LOCATION "/jackd";
  357. strncpy(arguments, JACK_LOCATION "/jackd -T -d alsa -d hw:0 -p 2048", 255);
  358. #endif
  359. } else {
  360. result = strcspn(arguments, " ");
  361. command = (char*)malloc(result+1);
  362. strncpy(command, arguments, result);
  363. command[result] = '\0';
  364. }
  365. argv = (char**)malloc(255);
  366. while(1) {
  367. result = strcspn(arguments+pos, " ");
  368. if (result == 0) {
  369. break;
  370. }
  371. argv[i] = (char*)malloc(result+1);
  372. strncpy(argv[i], arguments+pos, result);
  373. argv[i][result] = '\0';
  374. pos += result+1;
  375. ++i;
  376. }
  377. argv[i] = 0;
  378. execv (command, argv);
  379. /* If execv() succeeds, it does not return. There's no point
  380. * in calling jack_error() here in the child process. */
  381. perror ("exec of JACK server failed");
  382. }
  383. int
  384. start_server (void)
  385. {
  386. pid_t pid1, pid2;
  387. if (getenv("JACK_NO_START_SERVER")) {
  388. return 1;
  389. }
  390. switch (pid1 = fork()) {
  391. case -1:
  392. return 1;
  393. case 0:
  394. switch (pid2 = fork()) {
  395. case -1:
  396. return 1;
  397. case 0:
  398. _start_server();
  399. }
  400. _exit(0);
  401. }
  402. return 0;
  403. }
  404. static int
  405. jack_request_client (ClientType type, const char* client_name,
  406. const char* so_name, const char* so_data,
  407. jack_client_connect_result_t *res, int *req_fd)
  408. {
  409. jack_client_connect_request_t req;
  410. *req_fd = -1;
  411. memset (&req, 0, sizeof (req));
  412. if (strlen (client_name) >= sizeof (req.name)) {
  413. jack_error ("\"%s\" is too long to be used as a JACK client"
  414. " name.\n"
  415. "Please use %lu characters or less.",
  416. client_name, sizeof (req.name));
  417. return -1;
  418. }
  419. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  420. jack_error ("\"%s\" is too long to be used as a JACK shared"
  421. " object name.\n"
  422. "Please use %lu characters or less.",
  423. so_name, sizeof (req.object_path) - 1);
  424. return -1;
  425. }
  426. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  427. jack_error ("\"%s\" is too long to be used as a JACK shared"
  428. " object data string.\n"
  429. "Please use %lu characters or less.",
  430. so_data, sizeof (req.object_data) - 1);
  431. return -1;
  432. }
  433. if ((*req_fd = server_connect (0)) < 0) {
  434. if (start_server() == 0) {
  435. sleep(2);
  436. if ((*req_fd = server_connect (0)) < 0) {
  437. goto fail;
  438. }
  439. } else {
  440. goto fail;
  441. }
  442. }
  443. req.load = TRUE;
  444. req.type = type;
  445. snprintf (req.name, sizeof (req.name), "%s", client_name);
  446. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  447. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  448. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  449. jack_error ("cannot send request to jack server (%s)",
  450. strerror (errno));
  451. goto fail;
  452. }
  453. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  454. if (errno == 0) {
  455. /* server shut the socket */
  456. jack_error ("could not attach as client "
  457. "(duplicate client name?)");
  458. goto fail;
  459. }
  460. jack_error ("cannot read response from jack server (%s)",
  461. strerror (errno));
  462. goto fail;
  463. }
  464. if (res->status) {
  465. jack_error ("could not attach as client "
  466. "(duplicate client name?)");
  467. goto fail;
  468. }
  469. if (res->protocol_v != jack_protocol_version){
  470. jack_error ("application linked against incompatible libjack"
  471. " version.");
  472. goto fail;
  473. }
  474. switch (type) {
  475. case ClientDriver:
  476. case ClientInternal:
  477. close (*req_fd);
  478. *req_fd = -1;
  479. break;
  480. default:
  481. break;
  482. }
  483. return 0;
  484. fail:
  485. if (*req_fd >= 0) {
  486. close (*req_fd);
  487. *req_fd = -1;
  488. }
  489. return -1;
  490. }
  491. int
  492. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  493. {
  494. /* Lookup, attach and register the port/buffer segments in use
  495. * right now.
  496. */
  497. if (client->control->type != ClientExternal) {
  498. jack_error("Only external clients need attach port segments");
  499. abort();
  500. }
  501. /* make sure we have space to store the port
  502. segment information.
  503. */
  504. if (ptid >= client->n_port_types) {
  505. client->port_segment = (jack_shm_info_t*)
  506. realloc (client->port_segment,
  507. sizeof (jack_shm_info_t) * (ptid+1));
  508. memset (&client->port_segment[client->n_port_types],
  509. 0,
  510. sizeof (jack_shm_info_t) *
  511. (ptid - client->n_port_types));
  512. client->n_port_types = ptid + 1;
  513. } else {
  514. /* release any previous segment */
  515. jack_release_shm (&client->port_segment[ptid]);
  516. }
  517. /* get the index into the shm registry */
  518. client->port_segment[ptid].index =
  519. client->engine->port_types[ptid].shm_registry_index;
  520. /* attach the relevant segment */
  521. if (jack_attach_shm (&client->port_segment[ptid])) {
  522. jack_error ("cannot attach port segment shared memory"
  523. " (%s)", strerror (errno));
  524. return -1;
  525. }
  526. /* The first chunk of the audio port segment will be set by
  527. * the engine to be a zero-filled buffer. This hasn't been
  528. * done yet, but it will happen before the process cycle
  529. * (re)starts.
  530. */
  531. if (ptid == JACK_AUDIO_PORT_TYPE) {
  532. jack_zero_filled_buffer =
  533. jack_shm_addr (&client->port_segment[ptid]);
  534. }
  535. return 0;
  536. }
  537. jack_client_t *
  538. jack_client_new (const char *client_name)
  539. {
  540. int req_fd = -1;
  541. int ev_fd = -1;
  542. jack_client_connect_result_t res;
  543. jack_client_t *client;
  544. jack_port_type_id_t ptid;
  545. /* external clients need this initialized; internal clients
  546. will use the setup in the server's address space.
  547. */
  548. jack_init_time ();
  549. if (jack_initialize_shm ()) {
  550. jack_error ("Unable to initialize shared memory.");
  551. return NULL;
  552. }
  553. if (jack_request_client (ClientExternal, client_name, "", "",
  554. &res, &req_fd)) {
  555. return NULL;
  556. }
  557. client = jack_client_alloc ();
  558. strcpy (client->fifo_prefix, res.fifo_prefix);
  559. client->request_fd = req_fd;
  560. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  561. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  562. /* attach the engine control/info block */
  563. client->engine_shm = res.engine_shm;
  564. if (jack_attach_shm (&client->engine_shm)) {
  565. jack_error ("cannot attached engine control shared memory"
  566. " segment");
  567. goto fail;
  568. }
  569. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  570. /* now attach the client control block */
  571. client->control_shm = res.client_shm;
  572. if (jack_attach_shm (&client->control_shm)) {
  573. jack_error ("cannot attached client control shared memory"
  574. " segment");
  575. goto fail;
  576. }
  577. client->control = (jack_client_control_t *)
  578. jack_shm_addr (&client->control_shm);
  579. /* nobody else needs to access this shared memory any more, so
  580. destroy it. because we have our own attachment to it, it won't
  581. vanish till we exit (and release it).
  582. */
  583. jack_destroy_shm (&client->control_shm);
  584. client->n_port_types = client->engine->n_port_types;
  585. client->port_segment = (jack_shm_info_t *)
  586. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  587. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  588. client->port_segment[ptid].index =
  589. client->engine->port_types[ptid].shm_registry_index;
  590. jack_attach_port_segment (client, ptid);
  591. }
  592. /* set up the client so that it does the right thing for an
  593. * external client
  594. */
  595. client->control->deliver_request = oop_client_deliver_request;
  596. client->control->deliver_arg = client;
  597. if ((ev_fd = server_event_connect (client)) < 0) {
  598. goto fail;
  599. }
  600. client->event_fd = ev_fd;
  601. #if defined(__APPLE__) && defined(__POWERPC__)
  602. /* specific resources for server/client real-time thread
  603. * communication */
  604. client->clienttask = mach_task_self();
  605. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  606. jack_error ("Can't find bootstrap port");
  607. goto fail;
  608. }
  609. if (allocate_mach_clientport(client, res.portnum) < 0) {
  610. jack_error("Can't allocate mach port");
  611. goto fail;
  612. };
  613. #endif
  614. return client;
  615. fail:
  616. if (client->engine) {
  617. jack_release_shm (&client->engine_shm);
  618. client->engine = 0;
  619. }
  620. if (client->control) {
  621. jack_release_shm (&client->control_shm);
  622. client->control = 0;
  623. }
  624. if (req_fd >= 0) {
  625. close (req_fd);
  626. }
  627. if (ev_fd >= 0) {
  628. close (ev_fd);
  629. }
  630. return 0;
  631. }
  632. int
  633. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  634. {
  635. jack_client_connect_result_t res;
  636. int req_fd;
  637. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  638. }
  639. void
  640. jack_internal_client_close (const char *client_name)
  641. {
  642. jack_client_connect_request_t req;
  643. int fd;
  644. req.load = FALSE;
  645. snprintf (req.name, sizeof (req.name), "%s", client_name);
  646. if ((fd = server_connect (0)) < 0) {
  647. return;
  648. }
  649. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  650. jack_error ("cannot deliver ClientUnload request to JACK server.");
  651. }
  652. /* no response to this request */
  653. close (fd);
  654. return;
  655. }
  656. int
  657. jack_drop_real_time_scheduling (pthread_t thread)
  658. {
  659. struct sched_param rtparam;
  660. int x;
  661. memset (&rtparam, 0, sizeof (rtparam));
  662. rtparam.sched_priority = 0;
  663. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  664. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  665. return -1;
  666. }
  667. return 0;
  668. }
  669. int
  670. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  671. {
  672. struct sched_param rtparam;
  673. int x;
  674. memset (&rtparam, 0, sizeof (rtparam));
  675. rtparam.sched_priority = priority;
  676. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  677. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  678. "(%d: %s)", rtparam.sched_priority, x,
  679. strerror (x));
  680. return -1;
  681. }
  682. return 0;
  683. }
  684. int
  685. jack_set_freewheel (jack_client_t* client, int onoff)
  686. {
  687. jack_request_t request;
  688. request.type = onoff ? FreeWheel : StopFreeWheel;
  689. return jack_client_deliver_request (client, &request);
  690. }
  691. void
  692. jack_start_freewheel (jack_client_t* client)
  693. {
  694. jack_client_control_t *control = client->control;
  695. if (client->engine->real_time) {
  696. jack_drop_real_time_scheduling (client->thread);
  697. }
  698. if (control->freewheel_cb) {
  699. control->freewheel_cb (1, control->freewheel_arg);
  700. }
  701. }
  702. void
  703. jack_stop_freewheel (jack_client_t* client)
  704. {
  705. jack_client_control_t *control = client->control;
  706. if (control->freewheel_cb) {
  707. control->freewheel_cb (0, control->freewheel_arg);
  708. }
  709. if (client->engine->real_time) {
  710. jack_acquire_real_time_scheduling (client->thread,
  711. client->engine->client_priority);
  712. }
  713. }
  714. static void *
  715. jack_client_thread (void *arg)
  716. {
  717. jack_client_t *client = (jack_client_t *) arg;
  718. jack_client_control_t *control = client->control;
  719. jack_event_t event;
  720. char status = 0;
  721. char c;
  722. int err = 0;
  723. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  724. pthread_mutex_lock (&client_lock);
  725. client->thread_ok = TRUE;
  726. client->thread_id = pthread_self();
  727. pthread_cond_signal (&client_ready);
  728. pthread_mutex_unlock (&client_lock);
  729. client->control->pid = getpid();
  730. client->control->pgrp = getpgrp();
  731. DEBUG ("client thread is now running");
  732. while (err == 0) {
  733. if (client->engine->engine_ok == 0) {
  734. if (client->on_shutdown)
  735. client->on_shutdown (client->on_shutdown_arg);
  736. else
  737. jack_error ("engine unexpectedly shutdown; "
  738. "thread exiting\n");
  739. pthread_exit (0);
  740. }
  741. DEBUG ("client polling on event_fd and graph_wait_fd...");
  742. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  743. if (errno == EINTR) {
  744. continue;
  745. }
  746. jack_error ("poll failed in client (%s)",
  747. strerror (errno));
  748. status = -1;
  749. break;
  750. }
  751. /* get an accurate timestamp on waking from poll for a
  752. * process() cycle.
  753. */
  754. if (client->pollfd[1].revents & POLLIN) {
  755. control->awake_at = jack_get_microseconds();
  756. }
  757. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  758. client->pollfd[0].revents,
  759. client->pollfd[1].revents);
  760. pthread_testcancel();
  761. if ((client->pollfd[0].revents & ~POLLIN) ||
  762. client->control->dead) {
  763. goto zombie;
  764. }
  765. if (client->pollfd[0].revents & POLLIN) {
  766. DEBUG ("client receives an event, "
  767. "now reading on event fd");
  768. /* server has sent us an event. process the
  769. * event and reply */
  770. if (read (client->event_fd, &event, sizeof (event))
  771. != sizeof (event)) {
  772. jack_error ("cannot read server event (%s)",
  773. strerror (errno));
  774. err++;
  775. break;
  776. }
  777. status = 0;
  778. switch (event.type) {
  779. case PortRegistered:
  780. if (control->port_register) {
  781. control->port_register
  782. (event.x.port_id, TRUE,
  783. control->port_register_arg);
  784. }
  785. break;
  786. case PortUnregistered:
  787. if (control->port_register) {
  788. control->port_register
  789. (event.x.port_id, FALSE,
  790. control->port_register_arg);
  791. }
  792. break;
  793. case GraphReordered:
  794. status = jack_handle_reorder (client, &event);
  795. break;
  796. case PortConnected:
  797. case PortDisconnected:
  798. status = jack_client_handle_port_connection
  799. (client, &event);
  800. break;
  801. case BufferSizeChange:
  802. jack_client_invalidate_port_buffers (client);
  803. if (control->bufsize) {
  804. status = control->bufsize
  805. (control->nframes,
  806. control->bufsize_arg);
  807. }
  808. break;
  809. case SampleRateChange:
  810. if (control->srate) {
  811. status = control->srate
  812. (control->nframes,
  813. control->srate_arg);
  814. }
  815. break;
  816. case XRun:
  817. if (control->xrun) {
  818. status = control->xrun
  819. (control->xrun_arg);
  820. }
  821. break;
  822. case AttachPortSegment:
  823. jack_attach_port_segment (client, event.y.ptid);
  824. break;
  825. case StartFreewheel:
  826. jack_start_freewheel (client);
  827. break;
  828. case StopFreewheel:
  829. jack_stop_freewheel (client);
  830. break;
  831. }
  832. DEBUG ("client has dealt with the event, writing "
  833. "response on event fd");
  834. if (write (client->event_fd, &status, sizeof (status))
  835. != sizeof (status)) {
  836. jack_error ("cannot send event response to "
  837. "engine (%s)", strerror (errno));
  838. err++;
  839. break;
  840. }
  841. }
  842. if (client->pollfd[1].revents & ~POLLIN) {
  843. goto zombie;
  844. }
  845. if (client->pollfd[1].revents & POLLIN) {
  846. #ifdef WITH_TIMESTAMPS
  847. jack_reset_timestamps ();
  848. #endif
  849. DEBUG ("client %d signalled at %" PRIu64
  850. ", awake for process at %" PRIu64
  851. " (delay = %" PRIu64
  852. " usecs) (wakeup on graph_wait_fd==%d)",
  853. getpid(),
  854. control->signalled_at,
  855. control->awake_at,
  856. control->awake_at - control->signalled_at,
  857. client->pollfd[1].fd);
  858. control->state = Running;
  859. if (control->sync_cb)
  860. jack_call_sync_client (client);
  861. if (control->process) {
  862. if (control->process (control->nframes,
  863. control->process_arg)
  864. == 0) {
  865. control->state = Finished;
  866. }
  867. } else {
  868. control->state = Finished;
  869. }
  870. if (control->timebase_cb)
  871. jack_call_timebase_master (client);
  872. control->finished_at = jack_get_microseconds();
  873. #ifdef WITH_TIMESTAMPS
  874. jack_timestamp ("finished");
  875. #endif
  876. /* pass the execution token along */
  877. DEBUG ("client finished processing at %" PRIu64
  878. " (elapsed = %" PRIu64
  879. " usecs), writing on graph_next_fd==%d",
  880. control->finished_at,
  881. control->finished_at - control->awake_at,
  882. client->graph_next_fd);
  883. if (write (client->graph_next_fd, &c, sizeof (c))
  884. != sizeof (c)) {
  885. jack_error ("cannot continue execution of the "
  886. "processing graph (%s)",
  887. strerror(errno));
  888. err++;
  889. break;
  890. }
  891. DEBUG ("client sent message to next stage by %" PRIu64
  892. ", client reading on graph_wait_fd==%d",
  893. jack_get_microseconds(), client->graph_wait_fd);
  894. #ifdef WITH_TIMESTAMPS
  895. jack_timestamp ("read pending byte from wait");
  896. #endif
  897. DEBUG("reading cleanup byte from pipe\n");
  898. if ((read (client->graph_wait_fd, &c, sizeof (c))
  899. != sizeof (c))) {
  900. DEBUG ("WARNING: READ FAILED!");
  901. #if 0
  902. jack_error ("cannot complete execution of the "
  903. "processing graph (%s)",
  904. strerror(errno));
  905. err++;
  906. break;
  907. #endif
  908. }
  909. /* check if we were killed during the process
  910. * cycle (or whatever) */
  911. if (client->control->dead) {
  912. goto zombie;
  913. }
  914. DEBUG("process cycle fully complete\n");
  915. #ifdef WITH_TIMESTAMPS
  916. jack_timestamp ("read done");
  917. jack_dump_timestamps (stdout);
  918. #endif
  919. }
  920. }
  921. return (void *) ((intptr_t)err);
  922. zombie:
  923. if (client->on_shutdown) {
  924. jack_error ("zombified - calling shutdown handler");
  925. client->on_shutdown (client->on_shutdown_arg);
  926. } else {
  927. jack_error ("zombified - exiting from JACK");
  928. jack_client_close (client);
  929. /* Need a fix : possibly make client crash if
  930. * zombified without shutdown handler
  931. */
  932. }
  933. pthread_exit (0);
  934. /*NOTREACHED*/
  935. return 0;
  936. }
  937. #if defined(__APPLE__) && defined(__POWERPC__)
  938. /* real-time thread : separated from the normal client thread, it will
  939. * communicate with the server using fast mach RPC mechanism */
  940. static void *
  941. jack_client_process_thread (void *arg)
  942. {
  943. jack_client_t *client = (jack_client_t *) arg;
  944. jack_client_control_t *control = client->control;
  945. int err = 0;
  946. client->control->pid = getpid();
  947. DEBUG ("client process thread is now running");
  948. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  949. while (err == 0) {
  950. if (jack_client_suspend(client) < 0) {
  951. pthread_exit (0);
  952. }
  953. control->awake_at = jack_get_microseconds();
  954. DEBUG ("client resumed");
  955. control->state = Running;
  956. if (control->process) {
  957. if (control->process (control->nframes,
  958. control->process_arg) == 0) {
  959. control->state = Finished;
  960. }
  961. } else {
  962. control->state = Finished;
  963. }
  964. control->finished_at = jack_get_microseconds();
  965. #ifdef WITH_TIMESTAMPS
  966. jack_timestamp ("finished");
  967. #endif
  968. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  969. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  970. /* check if we were killed during the process cycle (or whatever) */
  971. if (client->control->dead) {
  972. jack_error ("jack_client_process_thread : client->control->dead");
  973. goto zombie;
  974. }
  975. DEBUG("process cycle fully complete\n");
  976. }
  977. return (void *) ((intptr_t)err);
  978. zombie:
  979. jack_error ("jack_client_process_thread : zombified");
  980. if (client->on_shutdown) {
  981. jack_error ("zombified - calling shutdown handler");
  982. client->on_shutdown (client->on_shutdown_arg);
  983. } else {
  984. jack_error ("zombified - exiting from JACK");
  985. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  986. }
  987. pthread_exit (0);
  988. /*NOTREACHED*/
  989. return 0;
  990. }
  991. #endif
  992. static int
  993. jack_start_thread (jack_client_t *client)
  994. {
  995. pthread_attr_t *attributes = 0;
  996. int policy = SCHED_OTHER;
  997. struct sched_param client_param, temp_param;
  998. if (client->engine->real_time) {
  999. /* Get the client thread to run as an RT-FIFO
  1000. scheduled thread of appropriate priority.
  1001. */
  1002. struct sched_param rt_param;
  1003. attributes = (pthread_attr_t *)
  1004. malloc (sizeof (pthread_attr_t));
  1005. pthread_attr_init (attributes);
  1006. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  1007. jack_error ("cannot set FIFO scheduling class for RT "
  1008. "thread");
  1009. return -1;
  1010. }
  1011. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1012. jack_error ("Cannot set scheduling scope for RT "
  1013. "thread");
  1014. return -1;
  1015. }
  1016. memset (&rt_param, 0, sizeof (rt_param));
  1017. rt_param.sched_priority = client->engine->client_priority;
  1018. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  1019. jack_error ("Cannot set scheduling priority for RT "
  1020. "thread (%s)", strerror (errno));
  1021. return -1;
  1022. }
  1023. #if defined(__APPLE__) && defined(__POWERPC__)
  1024. // To be implemented
  1025. #else
  1026. if (client->engine->do_mlock
  1027. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1028. jack_error ("cannot lock down memory for RT thread (%s)",
  1029. strerror (errno));
  1030. #ifdef ENSURE_MLOCK
  1031. return -1;
  1032. #endif /* ENSURE_MLOCK */
  1033. }
  1034. #endif
  1035. }
  1036. if (pthread_create (&client->thread, attributes,
  1037. jack_client_thread, client)) {
  1038. if (client->engine->real_time) {
  1039. /* we are probably dealing with a broken glibc so try
  1040. to work around the bug, see below for more details
  1041. */
  1042. goto realtime_workaround;
  1043. }
  1044. return -1;
  1045. }
  1046. #if defined(__APPLE__) && defined(__POWERPC__)
  1047. /* a spcial real-time thread to call the "process"
  1048. * callback. It will communicate with the server using fast
  1049. * mach RPC mechanism */
  1050. if (pthread_create (&client->process_thread, attributes,
  1051. jack_client_process_thread, client)) {
  1052. jack_error("pthread_create failed for process_thread \n");
  1053. return -1;
  1054. }
  1055. if (client->engine->real_time){
  1056. /* time constraint thread */
  1057. setThreadToPriority(client->process_thread, 96, true, 10000000);
  1058. }else{
  1059. /* fixed priority thread */
  1060. setThreadToPriority(client->process_thread, 63, true, 10000000);
  1061. }
  1062. #endif
  1063. return 0;
  1064. /* we get here only with engine running realtime */
  1065. realtime_workaround:
  1066. /* the version of glibc I've played with has a bug that makes
  1067. that code fail when running under a non-root user but with the
  1068. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  1069. does not check for capabilities, only for the uid being
  1070. zero). Newer versions apparently have this fixed. This
  1071. workaround temporarily switches the client thread to the
  1072. proper scheduler and priority, then starts the realtime
  1073. thread so that it can inherit them and finally switches the
  1074. client thread back to what it was before. Sigh. For ardour
  1075. I have to check again and switch the thread explicitly to
  1076. realtime, don't know why or how to debug - nando
  1077. */
  1078. /* get current scheduler and parameters of the client process */
  1079. if ((policy = sched_getscheduler (0)) < 0) {
  1080. jack_error ("Cannot get current client scheduler: %s",
  1081. strerror(errno));
  1082. return -1;
  1083. }
  1084. memset (&client_param, 0, sizeof (client_param));
  1085. if (sched_getparam (0, &client_param)) {
  1086. jack_error ("Cannot get current client scheduler "
  1087. "parameters: %s", strerror(errno));
  1088. return -1;
  1089. }
  1090. /* temporarily change the client process to SCHED_FIFO so that
  1091. the realtime thread can inherit the scheduler and priority
  1092. */
  1093. memset (&temp_param, 0, sizeof (temp_param));
  1094. temp_param.sched_priority = client->engine->client_priority;
  1095. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  1096. jack_error ("Cannot temporarily set client to RT scheduler:"
  1097. " %s", strerror(errno));
  1098. return -1;
  1099. }
  1100. /* prepare the attributes for the realtime thread */
  1101. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1102. pthread_attr_init (attributes);
  1103. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1104. sched_setscheduler (0, policy, &client_param);
  1105. jack_error ("Cannot set scheduling scope for RT thread");
  1106. return -1;
  1107. }
  1108. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1109. sched_setscheduler (0, policy, &client_param);
  1110. jack_error ("Cannot set scheduler inherit policy for RT "
  1111. "thread");
  1112. return -1;
  1113. }
  1114. /* create the RT thread */
  1115. if (pthread_create (&client->thread, attributes,
  1116. jack_client_thread, client)) {
  1117. sched_setscheduler (0, policy, &client_param);
  1118. return -1;
  1119. }
  1120. /* return the client process to the scheduler it was in before */
  1121. if (sched_setscheduler (0, policy, &client_param)) {
  1122. jack_error ("Cannot reset original client scheduler: %s",
  1123. strerror(errno));
  1124. return -1;
  1125. }
  1126. /* check again... inheritance of policy and priority works in
  1127. jack_simple_client but not in ardour! So I check again and
  1128. force the policy if it is not set correctly. This does not
  1129. really really work either, the manager thread of the
  1130. linuxthreads implementation is left running with
  1131. SCHED_OTHER, that is presumably very bad.
  1132. */
  1133. memset (&client_param, 0, sizeof (client_param));
  1134. if (pthread_getschedparam(client->thread, &policy,
  1135. &client_param) == 0) {
  1136. if (policy != SCHED_FIFO) {
  1137. memset (&client_param, 0, sizeof (client_param));
  1138. client_param.sched_priority =
  1139. client->engine->client_priority;
  1140. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1141. &client_param)) {
  1142. jack_error ("Cannot set (again) FIFO scheduling"
  1143. " class for RT thread\n");
  1144. return -1;
  1145. }
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. int
  1151. jack_activate (jack_client_t *client)
  1152. {
  1153. jack_request_t req;
  1154. /* we need to scribble on our stack to ensure that its memory
  1155. * pages are actually mapped (more important for mlockall(2)
  1156. * usage in jack_start_thread())
  1157. */
  1158. #if defined(__APPLE__) && defined(__POWERPC__)
  1159. /* a bigger stack makes the application crash... */
  1160. #define BIG_ENOUGH_STACK 10000
  1161. #else
  1162. #define BIG_ENOUGH_STACK 1048576
  1163. #endif
  1164. char buf[BIG_ENOUGH_STACK];
  1165. int i;
  1166. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  1167. buf[i] = (char) (i & 0xff);
  1168. }
  1169. #undef BIG_ENOUGH_STACK
  1170. if (client->control->type == ClientInternal ||
  1171. client->control->type == ClientDriver) {
  1172. goto startit;
  1173. }
  1174. /* get the pid of the client process to pass it to engine */
  1175. client->control->pid = getpid ();
  1176. #ifdef USE_CAPABILITIES
  1177. if (client->engine->has_capabilities != 0 &&
  1178. client->control->pid != 0 && client->engine->real_time != 0) {
  1179. /* we need to ask the engine for realtime capabilities
  1180. before trying to start the realtime thread
  1181. */
  1182. req.type = SetClientCapabilities;
  1183. req.x.client_id = client->control->id;
  1184. jack_client_deliver_request (client, &req);
  1185. if (req.status) {
  1186. /* what to do? engine is running realtime, it
  1187. is using capabilities and has them
  1188. (otherwise we would not get an error
  1189. return) but for some reason it could not
  1190. give the client the required capabilities,
  1191. so for now downgrade the client so that it
  1192. still runs, albeit non-realtime - nando
  1193. */
  1194. jack_error ("could not receive realtime capabilities, "
  1195. "client will run non-realtime");
  1196. /* XXX wrong, this is a property of the engine
  1197. client->engine->real_time = 0;
  1198. */
  1199. }
  1200. }
  1201. #endif /* USE_CAPABILITIES */
  1202. if (client->first_active) {
  1203. pthread_mutex_init (&client_lock, NULL);
  1204. pthread_cond_init (&client_ready, NULL);
  1205. pthread_mutex_lock (&client_lock);
  1206. if (jack_start_thread (client)) {
  1207. pthread_mutex_unlock (&client_lock);
  1208. return -1;
  1209. }
  1210. pthread_cond_wait (&client_ready, &client_lock);
  1211. pthread_mutex_unlock (&client_lock);
  1212. if (!client->thread_ok) {
  1213. jack_error ("could not start client thread");
  1214. return -1;
  1215. }
  1216. client->first_active = FALSE;
  1217. }
  1218. startit:
  1219. req.type = ActivateClient;
  1220. req.x.client_id = client->control->id;
  1221. return jack_client_deliver_request (client, &req);
  1222. }
  1223. int
  1224. jack_deactivate (jack_client_t *client)
  1225. {
  1226. jack_request_t req;
  1227. req.type = DeactivateClient;
  1228. req.x.client_id = client->control->id;
  1229. return jack_client_deliver_request (client, &req);
  1230. }
  1231. int
  1232. jack_client_close (jack_client_t *client)
  1233. {
  1234. JSList *node;
  1235. void *status;
  1236. if (client->control->active) {
  1237. jack_deactivate (client);
  1238. }
  1239. if (client->control->type == ClientExternal) {
  1240. /* stop the thread that communicates with the jack
  1241. * server, only if it was actually running
  1242. */
  1243. if (client->thread_ok){
  1244. pthread_cancel (client->thread);
  1245. pthread_join (client->thread, &status);
  1246. }
  1247. if (client->control) {
  1248. jack_release_shm (&client->control_shm);
  1249. client->control = NULL;
  1250. }
  1251. if (client->engine) {
  1252. jack_release_shm (&client->engine_shm);
  1253. client->engine = NULL;
  1254. }
  1255. if (client->port_segment) {
  1256. jack_port_type_id_t ptid;
  1257. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1258. jack_release_shm (&client->port_segment[ptid]);
  1259. }
  1260. free (client->port_segment);
  1261. client->port_segment = NULL;
  1262. }
  1263. if (client->graph_wait_fd) {
  1264. close (client->graph_wait_fd);
  1265. }
  1266. if (client->graph_next_fd) {
  1267. close (client->graph_next_fd);
  1268. }
  1269. close (client->event_fd);
  1270. close (client->request_fd);
  1271. }
  1272. for (node = client->ports; node; node = jack_slist_next (node)) {
  1273. free (node->data);
  1274. }
  1275. jack_slist_free (client->ports);
  1276. jack_client_free (client);
  1277. return 0;
  1278. }
  1279. int
  1280. jack_is_realtime (jack_client_t *client)
  1281. {
  1282. return client->engine->real_time;
  1283. }
  1284. jack_nframes_t
  1285. jack_get_buffer_size (jack_client_t *client)
  1286. {
  1287. return client->engine->buffer_size;
  1288. }
  1289. int
  1290. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1291. {
  1292. #ifdef DO_BUFFER_RESIZE
  1293. jack_request_t req;
  1294. req.type = SetBufferSize;
  1295. req.x.nframes = nframes;
  1296. return jack_client_deliver_request (client, &req);
  1297. #else
  1298. return ENOSYS;
  1299. #endif /* DO_BUFFER_RESIZE */
  1300. }
  1301. int
  1302. jack_connect (jack_client_t *client, const char *source_port,
  1303. const char *destination_port)
  1304. {
  1305. jack_request_t req;
  1306. req.type = ConnectPorts;
  1307. snprintf (req.x.connect.source_port,
  1308. sizeof (req.x.connect.source_port), "%s", source_port);
  1309. snprintf (req.x.connect.destination_port,
  1310. sizeof (req.x.connect.destination_port),
  1311. "%s", destination_port);
  1312. return jack_client_deliver_request (client, &req);
  1313. }
  1314. int
  1315. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1316. {
  1317. jack_request_t req;
  1318. pthread_mutex_lock (&port->connection_lock);
  1319. if (port->connections == NULL) {
  1320. pthread_mutex_unlock (&port->connection_lock);
  1321. return 0;
  1322. }
  1323. pthread_mutex_unlock (&port->connection_lock);
  1324. req.type = DisconnectPort;
  1325. req.x.port_info.port_id = port->shared->id;
  1326. return jack_client_deliver_request (client, &req);
  1327. }
  1328. int
  1329. jack_disconnect (jack_client_t *client, const char *source_port,
  1330. const char *destination_port)
  1331. {
  1332. jack_request_t req;
  1333. req.type = DisconnectPorts;
  1334. snprintf (req.x.connect.source_port,
  1335. sizeof (req.x.connect.source_port), "%s", source_port);
  1336. snprintf (req.x.connect.destination_port,
  1337. sizeof (req.x.connect.destination_port),
  1338. "%s", destination_port);
  1339. return jack_client_deliver_request (client, &req);
  1340. }
  1341. void
  1342. jack_set_error_function (void (*func) (const char *))
  1343. {
  1344. jack_error_callback = func;
  1345. }
  1346. int
  1347. jack_set_graph_order_callback (jack_client_t *client,
  1348. JackGraphOrderCallback callback, void *arg)
  1349. {
  1350. if (client->control->active) {
  1351. jack_error ("You cannot set callbacks on an active client.");
  1352. return -1;
  1353. }
  1354. client->control->graph_order = callback;
  1355. client->control->graph_order_arg = arg;
  1356. return 0;
  1357. }
  1358. int jack_set_xrun_callback (jack_client_t *client,
  1359. JackXRunCallback callback, void *arg)
  1360. {
  1361. if (client->control->active) {
  1362. jack_error ("You cannot set callbacks on an active client.");
  1363. return -1;
  1364. }
  1365. client->control->xrun = callback;
  1366. client->control->xrun_arg = arg;
  1367. return 0;
  1368. }
  1369. int
  1370. jack_set_process_callback (jack_client_t *client,
  1371. JackProcessCallback callback, void *arg)
  1372. {
  1373. if (client->control->active) {
  1374. jack_error ("You cannot set callbacks on an active client.");
  1375. return -1;
  1376. }
  1377. client->control->process_arg = arg;
  1378. client->control->process = callback;
  1379. return 0;
  1380. }
  1381. int
  1382. jack_set_freewheel_callback (jack_client_t *client,
  1383. JackFreewheelCallback callback, void *arg)
  1384. {
  1385. if (client->control->active) {
  1386. jack_error ("You cannot set callbacks on an active client.");
  1387. return -1;
  1388. }
  1389. client->control->freewheel_arg = arg;
  1390. client->control->freewheel_cb = callback;
  1391. return 0;
  1392. }
  1393. int
  1394. jack_set_buffer_size_callback (jack_client_t *client,
  1395. JackBufferSizeCallback callback, void *arg)
  1396. {
  1397. client->control->bufsize_arg = arg;
  1398. client->control->bufsize = callback;
  1399. return 0;
  1400. }
  1401. int
  1402. jack_set_port_registration_callback(jack_client_t *client,
  1403. JackPortRegistrationCallback callback,
  1404. void *arg)
  1405. {
  1406. if (client->control->active) {
  1407. jack_error ("You cannot set callbacks on an active client.");
  1408. return -1;
  1409. }
  1410. client->control->port_register_arg = arg;
  1411. client->control->port_register = callback;
  1412. return 0;
  1413. }
  1414. int
  1415. jack_get_process_start_fd (jack_client_t *client)
  1416. {
  1417. /* once this has been called, the client thread
  1418. does not sleep on the graph wait fd.
  1419. */
  1420. client->pollmax = 1;
  1421. return client->graph_wait_fd;
  1422. }
  1423. int
  1424. jack_get_process_done_fd (jack_client_t *client)
  1425. {
  1426. return client->graph_next_fd;
  1427. }
  1428. void
  1429. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1430. {
  1431. client->on_shutdown = function;
  1432. client->on_shutdown_arg = arg;
  1433. }
  1434. const char **
  1435. jack_get_ports (jack_client_t *client,
  1436. const char *port_name_pattern,
  1437. const char *type_name_pattern,
  1438. unsigned long flags)
  1439. {
  1440. jack_control_t *engine;
  1441. const char **matching_ports;
  1442. unsigned long match_cnt;
  1443. jack_port_shared_t *psp;
  1444. unsigned long i;
  1445. regex_t port_regex;
  1446. regex_t type_regex;
  1447. int matching;
  1448. engine = client->engine;
  1449. if (port_name_pattern && port_name_pattern[0]) {
  1450. regcomp (&port_regex, port_name_pattern,
  1451. REG_EXTENDED|REG_NOSUB);
  1452. }
  1453. if (type_name_pattern && type_name_pattern[0]) {
  1454. regcomp (&type_regex, type_name_pattern,
  1455. REG_EXTENDED|REG_NOSUB);
  1456. }
  1457. psp = engine->ports;
  1458. match_cnt = 0;
  1459. matching_ports = (const char **)
  1460. malloc (sizeof (char *) * engine->port_max);
  1461. for (i = 0; i < engine->port_max; i++) {
  1462. matching = 1;
  1463. if (!psp[i].in_use) {
  1464. continue;
  1465. }
  1466. if (flags) {
  1467. if ((psp[i].flags & flags) != flags) {
  1468. matching = 0;
  1469. }
  1470. }
  1471. if (matching && port_name_pattern && port_name_pattern[0]) {
  1472. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1473. matching = 0;
  1474. }
  1475. }
  1476. if (matching && type_name_pattern && type_name_pattern[0]) {
  1477. jack_port_type_id_t ptid = psp[i].ptype_id;
  1478. if (regexec (&type_regex,
  1479. engine->port_types[ptid].type_name,
  1480. 0, NULL, 0)) {
  1481. matching = 0;
  1482. }
  1483. }
  1484. if (matching) {
  1485. matching_ports[match_cnt++] = psp[i].name;
  1486. }
  1487. }
  1488. matching_ports[match_cnt] = 0;
  1489. if (match_cnt == 0) {
  1490. free (matching_ports);
  1491. matching_ports = 0;
  1492. }
  1493. return matching_ports;
  1494. }
  1495. float
  1496. jack_cpu_load (jack_client_t *client)
  1497. {
  1498. return client->engine->cpu_load;
  1499. }
  1500. pthread_t
  1501. jack_client_thread_id (jack_client_t *client)
  1502. {
  1503. return client->thread_id;
  1504. }
  1505. int
  1506. jack_client_name_size(void)
  1507. {
  1508. return JACK_CLIENT_NAME_SIZE;
  1509. }
  1510. int
  1511. jack_port_name_size(void)
  1512. {
  1513. return JACK_PORT_NAME_SIZE;
  1514. }
  1515. int
  1516. jack_port_type_size(void)
  1517. {
  1518. return JACK_PORT_TYPE_SIZE;
  1519. }
  1520. #if defined(__APPLE__) && defined(__POWERPC__)
  1521. double __jack_time_ratio;
  1522. void jack_init_time ()
  1523. {
  1524. mach_timebase_info_data_t info;
  1525. mach_timebase_info(&info);
  1526. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1527. }
  1528. #else
  1529. jack_time_t
  1530. jack_get_mhz (void)
  1531. {
  1532. FILE *f = fopen("/proc/cpuinfo", "r");
  1533. if (f == 0)
  1534. {
  1535. perror("can't open /proc/cpuinfo\n");
  1536. exit(1);
  1537. }
  1538. for ( ; ; )
  1539. {
  1540. jack_time_t mhz;
  1541. int ret;
  1542. char buf[1000];
  1543. if (fgets(buf, sizeof(buf), f) == NULL) {
  1544. jack_error ("FATAL: cannot locate cpu MHz in "
  1545. "/proc/cpuinfo\n");
  1546. exit(1);
  1547. }
  1548. #if defined(__powerpc__)
  1549. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  1550. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  1551. defined(__x86_64__)
  1552. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  1553. #elif defined( __sparc__ )
  1554. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  1555. #elif defined( __mc68000__ )
  1556. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  1557. #elif defined( __s390__ )
  1558. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  1559. #else /* MIPS, ARM, alpha */
  1560. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  1561. #endif
  1562. if (ret == 1)
  1563. {
  1564. fclose(f);
  1565. return (jack_time_t)mhz;
  1566. }
  1567. }
  1568. }
  1569. jack_time_t __jack_cpu_mhz;
  1570. void jack_init_time ()
  1571. {
  1572. __jack_cpu_mhz = jack_get_mhz ();
  1573. }
  1574. #endif