Revert "metadata: Make JACK_METADATA_* constant"
[jack2.git] / tools / lsp.c
bloba1e3f1aa808521e50705dfbf267b88a9bc2cfe78
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>
26 #include <jack/jack.h>
27 #include <jack/session.h>
28 #include <jack/uuid.h>
30 char * my_name;
32 static void
33 show_version (void)
35 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
38 static void
39 printf_name2uuid (jack_client_t* client, const char* pname)
41 char *port_component = strchr( pname, ':' );
42 size_t csize = port_component - pname + 1;
43 char client_component[csize];
44 snprintf(client_component, csize, "%s", pname);
46 char *uuid = jack_get_uuid_for_client_name(client, client_component);
47 if (uuid) {
48 printf("%s%s\n", uuid, port_component );
49 } else {
50 printf("%s\n",pname);
52 jack_free(uuid);
55 static void
56 show_usage (void)
58 show_version ();
59 fprintf (stderr, "\nUsage: %s [options] [filter string]\n", my_name);
60 fprintf (stderr, "List active Jack ports, and optionally display extra information.\n");
61 fprintf (stderr, "Optionally filter ports which match ALL strings provided after any options.\n\n");
62 fprintf (stderr, "Display options:\n");
63 fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
64 fprintf (stderr, " -A, --aliases List aliases for each port\n");
65 fprintf (stderr, " -c, --connections List connections to/from each port\n");
66 fprintf (stderr, " -l, --port-latency Display per-port latency in frames at each port\n");
67 fprintf (stderr, " -L, --total-latency Display total latency in frames at each port\n");
68 fprintf (stderr, " -p, --properties Display port properties. Output may include:\n"
69 " input|output, can-monitor, physical, terminal\n\n");
70 fprintf (stderr, " -t, --type Display port type\n");
71 fprintf (stderr, " -u, --uuid Display uuid instead of client name (if available)\n");
72 fprintf (stderr, " -U, --port-uuid Display port uuid\n");
73 fprintf (stderr, " -h, --help Display this help message\n");
74 fprintf (stderr, " --version Output version information and exit\n\n");
75 fprintf (stderr, "For more information see http://jackaudio.org/\n");
78 int
79 main (int argc, char *argv[])
81 jack_client_t *client;
82 jack_status_t status;
83 jack_options_t options = JackNoStartServer;
84 const char **ports, **connections;
85 unsigned int i, j, k;
86 int skip_port;
87 int show_aliases = 0;
88 int show_con = 0;
89 int show_port_latency = 0;
90 int show_total_latency = 0;
91 int show_properties = 0;
92 int show_type = 0;
93 int show_uuid = 0;
94 int show_port_uuid = 0;
95 int c;
96 int option_index;
97 char* aliases[2];
98 char *server_name = NULL;
100 struct option long_options[] = {
101 { "server", 1, 0, 's' },
102 { "aliases", 0, 0, 'A' },
103 { "connections", 0, 0, 'c' },
104 { "port-latency", 0, 0, 'l' },
105 { "total-latency", 0, 0, 'L' },
106 { "properties", 0, 0, 'p' },
107 { "type", 0, 0, 't' },
108 { "uuid", 0, 0, 'u' },
109 { "port-uuid", 0, 0, 'U' },
110 { "help", 0, 0, 'h' },
111 { "version", 0, 0, 'v' },
112 { 0, 0, 0, 0 }
115 my_name = strrchr(argv[0], '/');
116 if (my_name == 0) {
117 my_name = argv[0];
118 } else {
119 my_name ++;
122 while ((c = getopt_long (argc, argv, "s:AclLphvtuU", long_options, &option_index)) >= 0) {
123 switch (c) {
124 case 's':
125 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
126 strcpy (server_name, optarg);
127 options |= JackServerName;
128 break;
129 case 'A':
130 aliases[0] = (char *) malloc (jack_port_name_size());
131 aliases[1] = (char *) malloc (jack_port_name_size());
132 show_aliases = 1;
133 break;
134 case 'c':
135 show_con = 1;
136 break;
137 case 'l':
138 show_port_latency = 1;
139 break;
140 case 'L':
141 show_total_latency = 1;
142 break;
143 case 'p':
144 show_properties = 1;
145 break;
146 case 't':
147 show_type = 1;
148 break;
149 case 'u':
150 show_uuid = 1;
151 break;
152 case 'U':
153 show_port_uuid = 1;
154 break;
155 case 'h':
156 show_usage ();
157 return 1;
158 break;
159 case 'v':
160 show_version ();
161 return 1;
162 break;
163 default:
164 show_usage ();
165 return 1;
166 break;
170 /* Open a client connection to the JACK server. Starting a
171 * new server only to list its ports seems pointless, so we
172 * specify JackNoStartServer. */
173 if ((client = jack_client_open ("lsp", options, &status, server_name)) == 0) {
174 fprintf (stderr, "Error: cannot connect to JACK, ");
175 if (status & JackServerFailed) {
176 fprintf (stderr, "server is not running.\n");
177 } else {
178 fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n", status);
180 return 1;
183 ports = jack_get_ports (client, NULL, NULL, 0);
185 for (i = 0; ports && ports[i]; ++i) {
186 // skip over any that don't match ALL of the strings presented at command line
187 skip_port = 0;
188 for (k = optind; k < argc; k++){
189 if (strstr(ports[i], argv[k]) == NULL ){
190 skip_port = 1;
193 if (skip_port) continue;
195 if (show_uuid) {
196 printf_name2uuid(client, ports[i]);
197 } else {
198 printf ("%s\n", ports[i]);
201 jack_port_t *port = jack_port_by_name (client, ports[i]);
203 if (show_port_uuid) {
204 char buf[JACK_UUID_STRING_SIZE];
205 jack_uuid_t uuid = jack_port_uuid (port);
206 jack_uuid_unparse (uuid, buf);
207 printf (" uuid: %s\n", buf);
210 if (show_aliases) {
211 int cnt;
212 int i;
214 cnt = jack_port_get_aliases (port, aliases);
215 for (i = 0; i < cnt; ++i) {
216 printf (" %s\n", aliases[i]);
220 if (show_con) {
221 if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
222 for (j = 0; connections[j]; j++) {
223 printf(" ");
224 if (show_uuid) {
225 printf_name2uuid(client, connections[j]);
226 } else {
227 printf("%s\n", connections[j]);
230 jack_free (connections);
233 if (show_port_latency) {
234 if (port) {
235 jack_latency_range_t range;
237 jack_port_get_latency_range (port, JackPlaybackLatency, &range);
238 printf (" port playback latency = [ %" PRIu32 " %" PRIu32 " ] frames\n",
239 range.min, range.max);
241 jack_port_get_latency_range (port, JackCaptureLatency, &range);
242 printf (" port capture latency = [ %" PRIu32 " %" PRIu32 " ] frames\n",
243 range.min, range.max);
246 if (show_total_latency) {
247 if (port) {
248 printf (" total latency = %d frames\n",
249 jack_port_get_total_latency (client, port));
252 if (show_properties) {
253 if (port) {
254 int flags = jack_port_flags (port);
255 printf (" properties: ");
256 if (flags & JackPortIsInput) {
257 fputs ("input,", stdout);
259 if (flags & JackPortIsOutput) {
260 fputs ("output,", stdout);
262 if (flags & JackPortCanMonitor) {
263 fputs ("can-monitor,", stdout);
265 if (flags & JackPortIsPhysical) {
266 fputs ("physical,", stdout);
268 if (flags & JackPortIsTerminal) {
269 fputs ("terminal,", stdout);
271 putc ('\n', stdout);
274 if (show_type) {
275 if (port) {
276 putc ('\t', stdout);
277 fputs (jack_port_type (port), stdout);
278 putc ('\n', stdout);
283 if (show_aliases) {
284 free(aliases[0]);
285 free(aliases[1]);
287 if (ports)
288 jack_free (ports);
289 jack_client_close (client);
290 exit (0);