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.

1744 lines
42KB

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