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.

1848 lines
43KB

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