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.

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