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.

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