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.

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