Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / example-clients / lsp.c
bloba8990a58e047c2cfbf4535f962bf96820bb31f21
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #ifndef WIN32
20 #include <unistd.h>
21 #endif
22 #include <string.h>
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <jack/jack.h>
27 char * my_name;
29 static void
30 show_version (void)
32 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
35 static void
36 show_usage (void)
38 show_version ();
39 fprintf (stderr, "\nUsage: %s [options] [filter string]\n", my_name);
40 fprintf (stderr, "List active Jack ports, and optionally display extra information.\n");
41 fprintf (stderr, "Optionally filter ports which match ALL strings provided after any options.\n\n");
42 fprintf (stderr, "Display options:\n");
43 fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
44 fprintf (stderr, " -A, --aliases List aliases for each port\n");
45 fprintf (stderr, " -c, --connections List connections to/from each port\n");
46 fprintf (stderr, " -l, --latency Display per-port latency in frames at each port\n");
47 fprintf (stderr, " -L, --latency Display total latency in frames at each port\n");
48 fprintf (stderr, " -p, --properties Display port properties. Output may include:\n"
49 " input|output, can-monitor, physical, terminal\n\n");
50 fprintf (stderr, " -t, --type Display port type\n");
51 fprintf (stderr, " -h, --help Display this help message\n");
52 fprintf (stderr, " --version Output version information and exit\n\n");
53 fprintf (stderr, "For more information see http://jackaudio.org/\n");
56 int
57 main (int argc, char *argv[])
59 jack_client_t *client;
60 jack_status_t status;
61 jack_options_t options = JackNoStartServer;
62 const char **ports, **connections;
63 unsigned int i, j, k;
64 int skip_port;
65 int show_aliases = 0;
66 int show_con = 0;
67 int show_port_latency = 0;
68 int show_total_latency = 0;
69 int show_properties = 0;
70 int show_type = 0;
71 int c;
72 int option_index;
73 char* aliases[2];
74 char *server_name = NULL;
75 jack_port_t *port;
77 struct option long_options[] = {
78 { "server", 1, 0, 's' },
79 { "aliases", 0, 0, 'A' },
80 { "connections", 0, 0, 'c' },
81 { "port-latency", 0, 0, 'l' },
82 { "total-latency", 0, 0, 'L' },
83 { "properties", 0, 0, 'p' },
84 { "type", 0, 0, 't' },
85 { "help", 0, 0, 'h' },
86 { "version", 0, 0, 'v' },
87 { 0, 0, 0, 0 }
90 my_name = strrchr(argv[0], '/');
91 if (my_name == 0) {
92 my_name = argv[0];
93 } else {
94 my_name ++;
97 while ((c = getopt_long (argc, argv, "s:AclLphvt", long_options, &option_index)) >= 0) {
98 switch (c) {
99 case 's':
100 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
101 strcpy (server_name, optarg);
102 options |= JackServerName;
103 break;
104 case 'A':
105 aliases[0] = (char *) malloc (jack_port_name_size());
106 aliases[1] = (char *) malloc (jack_port_name_size());
107 show_aliases = 1;
108 break;
109 case 'c':
110 show_con = 1;
111 break;
112 case 'l':
113 show_port_latency = 1;
114 break;
115 case 'L':
116 show_total_latency = 1;
117 break;
118 case 'p':
119 show_properties = 1;
120 break;
121 case 't':
122 show_type = 1;
123 break;
124 case 'h':
125 show_usage ();
126 return 1;
127 break;
128 case 'v':
129 show_version ();
130 return 1;
131 break;
132 default:
133 show_usage ();
134 return 1;
135 break;
139 /* Open a client connection to the JACK server. Starting a
140 * new server only to list its ports seems pointless, so we
141 * specify JackNoStartServer. */
142 //JOQ: need a new server name option
144 client = jack_client_open ("lsp", options, &status, server_name);
145 if (client == NULL) {
146 if (status & JackServerFailed) {
147 fprintf (stderr, "JACK server not running\n");
148 } else {
149 fprintf (stderr, "jack_client_open() failed, "
150 "status = 0x%2.0x\n", status);
152 return 1;
155 ports = jack_get_ports (client, NULL, NULL, 0);
156 if (!ports)
157 goto error;
159 for (i = 0; ports && ports[i]; ++i) {
160 // skip over any that don't match ALL of the strings presented at command line
161 skip_port = 0;
162 for(k = optind; k < argc; k++){
163 if(strstr(ports[i], argv[k]) == NULL ){
164 skip_port = 1;
167 if (skip_port) continue;
169 printf ("%s\n", ports[i]);
170 port = jack_port_by_name (client, ports[i]);
172 if (show_aliases) {
173 int cnt;
174 int i;
176 cnt = jack_port_get_aliases (port, aliases);
177 for (i = 0; i < cnt; ++i) {
178 printf (" %s\n", aliases[i]);
182 if (show_con) {
183 if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
184 for (j = 0; connections[j]; j++) {
185 printf (" %s\n", connections[j]);
187 free (connections);
190 if (show_port_latency) {
191 if (port) {
192 jack_latency_range_t range;
193 printf (" port latency = %" PRIu32 " frames\n",
194 jack_port_get_latency (port));
196 jack_port_get_latency_range (port, JackPlaybackLatency, &range);
197 printf (" port playback latency = [ %" PRIu32 " %" PRIu32 " ] frames\n",
198 range.min, range.max);
200 jack_port_get_latency_range (port, JackCaptureLatency, &range);
201 printf (" port capture latency = [ %" PRIu32 " %" PRIu32 " ] frames\n",
202 range.min, range.max);
205 if (show_total_latency) {
206 if (port) {
207 printf (" total latency = %d frames\n",
208 jack_port_get_total_latency (client, port));
211 if (show_properties) {
212 if (port) {
213 int flags = jack_port_flags (port);
214 printf (" properties: ");
215 if (flags & JackPortIsInput) {
216 fputs ("input,", stdout);
218 if (flags & JackPortIsOutput) {
219 fputs ("output,", stdout);
221 if (flags & JackPortCanMonitor) {
222 fputs ("can-monitor,", stdout);
224 if (flags & JackPortIsPhysical) {
225 fputs ("physical,", stdout);
227 if (flags & JackPortIsTerminal) {
228 fputs ("terminal,", stdout);
231 putc ('\n', stdout);
234 if (show_type) {
235 if (port) {
236 putc ('\t', stdout);
237 fputs (jack_port_type (port), stdout);
238 putc ('\n', stdout);
243 error:
244 if (show_aliases) {
245 free(aliases[0]);
246 free(aliases[1]);
248 if (ports)
249 jack_free (ports);
250 jack_client_close (client);
251 exit (0);