fix building when PATH_MAX is not defined. (should fix debian bug 320736)
[jack.git] / tools / lsp.c
blob450df32437d319325a430f7d0be7444a3f1b5548
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <getopt.h>
7 #include <config.h>
9 #include <jack/jack.h>
11 char * my_name;
13 void
14 show_version (void)
16 fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n",
17 my_name);
20 void
21 show_usage (void)
23 show_version ();
24 fprintf (stderr, "\nUsage: %s [options] [filter string]\n", my_name);
25 fprintf (stderr, "List active Jack ports, and optionally display extra information.\n");
26 fprintf (stderr, "Optionally filter ports which match ALL strings provided after any options.\n\n");
27 fprintf (stderr, "Display options:\n");
28 fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
29 fprintf (stderr, " -A, --aliases List aliases for each port\n");
30 fprintf (stderr, " -c, --connections List connections to/from each port\n");
31 fprintf (stderr, " -l, --latency Display per-port latency in frames at each port\n");
32 fprintf (stderr, " -L, --latency Display total latency in frames at each port\n");
33 fprintf (stderr, " -p, --properties Display port properties. Output may include:\n"
34 " input|output, can-monitor, physical, terminal\n\n");
35 fprintf (stderr, " -t, --type Display port type\n");
36 fprintf (stderr, " -h, --help Display this help message\n");
37 fprintf (stderr, " --version Output version information and exit\n\n");
38 fprintf (stderr, "For more information see http://jackaudio.org/\n");
41 int
42 main (int argc, char *argv[])
44 jack_client_t *client;
45 jack_status_t status;
46 jack_options_t options = JackNoStartServer;
47 const char **ports, **connections;
48 unsigned int i, j, k;
49 int skip_port;
50 int show_aliases = 0;
51 int show_con = 0;
52 int show_port_latency = 0;
53 int show_total_latency = 0;
54 int show_properties = 0;
55 int show_type = 0;
56 int c;
57 int option_index;
58 char* aliases[2];
59 char *server_name = NULL;
62 struct option long_options[] = {
63 { "server", 1, 0, 's' },
64 { "aliases", 0, 0, 'A' },
65 { "connections", 0, 0, 'c' },
66 { "port-latency", 0, 0, 'l' },
67 { "total-latency", 0, 0, 'L' },
68 { "properties", 0, 0, 'p' },
69 { "type", 0, 0, 't' },
70 { "help", 0, 0, 'h' },
71 { "version", 0, 0, 'v' },
72 { 0, 0, 0, 0 }
75 my_name = strrchr(argv[0], '/');
76 if (my_name == 0) {
77 my_name = argv[0];
78 } else {
79 my_name ++;
82 while ((c = getopt_long (argc, argv, "s:AclLphvt", long_options, &option_index)) >= 0) {
83 switch (c) {
84 case 's':
85 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
86 strcpy (server_name, optarg);
87 options |= JackServerName;
88 break;
89 case 'A':
90 aliases[0] = (char *) malloc (jack_port_name_size());
91 aliases[1] = (char *) malloc (jack_port_name_size());
92 show_aliases = 1;
93 break;
94 case 'c':
95 show_con = 1;
96 break;
97 case 'l':
98 show_port_latency = 1;
99 break;
100 case 'L':
101 show_total_latency = 1;
102 break;
103 case 'p':
104 show_properties = 1;
105 break;
106 case 't':
107 show_type = 1;
108 break;
109 case 'h':
110 show_usage ();
111 return 1;
112 break;
113 case 'v':
114 show_version ();
115 return 1;
116 break;
117 default:
118 show_usage ();
119 return 1;
120 break;
124 /* Open a client connection to the JACK server. Starting a
125 * new server only to list its ports seems pointless, so we
126 * specify JackNoStartServer. */
127 client = jack_client_open ("lsp", options, &status, server_name);
128 if (client == NULL) {
129 if (status & JackServerFailed) {
130 fprintf (stderr, "JACK server not running\n");
131 } else {
132 fprintf (stderr, "jack_client_open() failed, "
133 "status = 0x%2.0x\n", status);
135 return 1;
138 ports = jack_get_ports (client, NULL, NULL, 0);
140 for (i = 0; ports[i]; ++i) {
141 // skip over any that don't match ALL of the strings presented at command line
142 skip_port = 0;
143 for(k=optind; k < argc; k++){
144 if(strstr(ports[i], argv[k]) == NULL ){
145 skip_port = 1;
148 if(skip_port) continue;
150 printf ("%s\n", ports[i]);
152 jack_port_t *port = jack_port_by_name (client, ports[i]);
154 if (show_aliases) {
155 int cnt;
156 int i;
158 cnt = jack_port_get_aliases (port, aliases);
159 for (i = 0; i < cnt; ++i) {
160 printf (" %s\n", aliases[i]);
164 if (show_con) {
165 if ((connections = jack_port_get_all_connections (client, jack_port_by_name(client, ports[i]))) != 0) {
166 for (j = 0; connections[j]; j++) {
167 printf (" %s\n", connections[j]);
169 free (connections);
172 if (show_port_latency) {
173 if (port) {
174 printf (" port latency = %" PRIu32 " frames\n",
175 jack_port_get_latency (port));
178 if (show_total_latency) {
179 if (port) {
180 printf (" total latency = %" PRIu32 " frames\n",
181 jack_port_get_total_latency (client, port));
184 if (show_properties) {
185 if (port) {
186 int flags = jack_port_flags (port);
187 printf (" properties: ");
188 if (flags & JackPortIsInput) {
189 fputs ("input,", stdout);
191 if (flags & JackPortIsOutput) {
192 fputs ("output,", stdout);
194 if (flags & JackPortCanMonitor) {
195 fputs ("can-monitor,", stdout);
197 if (flags & JackPortIsPhysical) {
198 fputs ("physical,", stdout);
200 if (flags & JackPortIsTerminal) {
201 fputs ("terminal,", stdout);
203 putc ('\n', stdout);
206 if (show_type) {
207 if (port) {
208 putc ('\t', stdout);
209 fputs (jack_port_type (port), stdout);
210 putc ('\n', stdout);
214 jack_client_close (client);
215 exit (0);