Imported Upstream version 1.0.28
[platform/upstream/alsa-utils.git] / seq / aconnect / aconnect.c
1 /*
2  * connect / disconnect two subscriber ports
3  *   ver.0.1.3
4  *
5  * Copyright (C) 1999 Takashi Iwai
6  * 
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  * 
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  */
17
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <stdarg.h>
26 #include <locale.h>
27 #include <sys/ioctl.h>
28 #include <alsa/asoundlib.h>
29 #include "aconfig.h"
30 #include "gettext.h"
31
32 static void error_handler(const char *file, int line, const char *function, int err, const char *fmt, ...)
33 {
34         va_list arg;
35
36         if (err == ENOENT)      /* Ignore those misleading "warnings" */
37                 return;
38         va_start(arg, fmt);
39         fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
40         vfprintf(stderr, fmt, arg);
41         if (err)
42                 fprintf(stderr, ": %s", snd_strerror(err));
43         putc('\n', stderr);
44         va_end(arg);
45 }
46
47 static void usage(void)
48 {
49         printf(_("aconnect - ALSA sequencer connection manager\n"));
50         printf(_("Copyright (C) 1999-2000 Takashi Iwai\n"));
51         printf(_("Usage:\n"));
52         printf(_(" * Connection/disconnection between two ports\n"));
53         printf(_("   aconnect [-options] sender receiver\n"));
54         printf(_("     sender, receiver = client:port pair\n"));
55         printf(_("     -d,--disconnect     disconnect\n"));
56         printf(_("     -e,--exclusive      exclusive connection\n"));
57         printf(_("     -r,--real #         convert real-time-stamp on queue\n"));
58         printf(_("     -t,--tick #         convert tick-time-stamp on queue\n"));
59         printf(_(" * List connected ports (no subscription action)\n"));
60         printf(_("   aconnect -i|-o [-options]\n"));
61         printf(_("     -i,--input          list input (readable) ports\n"));
62         printf(_("     -o,--output         list output (writable) ports\n"));
63         printf(_("     -l,--list           list current connections of each port\n"));
64         printf(_(" * Remove all exported connections\n"));
65         printf(_("     -x, --removeall\n"));
66 }
67
68 /*
69  * check permission (capability) of specified port
70  */
71
72 #define LIST_INPUT      1
73 #define LIST_OUTPUT     2
74
75 #define perm_ok(pinfo,bits) ((snd_seq_port_info_get_capability(pinfo) & (bits)) == (bits))
76
77 static int check_permission(snd_seq_port_info_t *pinfo, int perm)
78 {
79         if (perm) {
80                 if (perm & LIST_INPUT) {
81                         if (perm_ok(pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ))
82                                 goto __ok;
83                 }
84                 if (perm & LIST_OUTPUT) {
85                         if (perm_ok(pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE))
86                                 goto __ok;
87                 }
88                 return 0;
89         }
90  __ok:
91         if (snd_seq_port_info_get_capability(pinfo) & SND_SEQ_PORT_CAP_NO_EXPORT)
92                 return 0;
93         return 1;
94 }
95
96 /*
97  * list subscribers of specified type
98  */
99 static void list_each_subs(snd_seq_t *seq, snd_seq_query_subscribe_t *subs, int type, const char *msg)
100 {
101         int count = 0;
102         snd_seq_query_subscribe_set_type(subs, type);
103         snd_seq_query_subscribe_set_index(subs, 0);
104         while (snd_seq_query_port_subscribers(seq, subs) >= 0) {
105                 const snd_seq_addr_t *addr;
106                 if (count++ == 0)
107                         printf("\t%s: ", msg);
108                 else
109                         printf(", ");
110                 addr = snd_seq_query_subscribe_get_addr(subs);
111                 printf("%d:%d", addr->client, addr->port);
112                 if (snd_seq_query_subscribe_get_exclusive(subs))
113                         printf("[ex]");
114                 if (snd_seq_query_subscribe_get_time_update(subs))
115                         printf("[%s:%d]",
116                                (snd_seq_query_subscribe_get_time_real(subs) ? "real" : "tick"),
117                                snd_seq_query_subscribe_get_queue(subs));
118                 snd_seq_query_subscribe_set_index(subs, snd_seq_query_subscribe_get_index(subs) + 1);
119         }
120         if (count > 0)
121                 printf("\n");
122 }
123
124 /*
125  * list subscribers
126  */
127 static void list_subscribers(snd_seq_t *seq, const snd_seq_addr_t *addr)
128 {
129         snd_seq_query_subscribe_t *subs;
130         snd_seq_query_subscribe_alloca(&subs);
131         snd_seq_query_subscribe_set_root(subs, addr);
132         list_each_subs(seq, subs, SND_SEQ_QUERY_SUBS_READ, _("Connecting To"));
133         list_each_subs(seq, subs, SND_SEQ_QUERY_SUBS_WRITE, _("Connected From"));
134 }
135
136 /*
137  * search all ports
138  */
139 typedef void (*action_func_t)(snd_seq_t *seq, snd_seq_client_info_t *cinfo, snd_seq_port_info_t *pinfo, int count);
140
141 static void do_search_port(snd_seq_t *seq, int perm, action_func_t do_action)
142 {
143         snd_seq_client_info_t *cinfo;
144         snd_seq_port_info_t *pinfo;
145         int count;
146
147         snd_seq_client_info_alloca(&cinfo);
148         snd_seq_port_info_alloca(&pinfo);
149         snd_seq_client_info_set_client(cinfo, -1);
150         while (snd_seq_query_next_client(seq, cinfo) >= 0) {
151                 /* reset query info */
152                 snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
153                 snd_seq_port_info_set_port(pinfo, -1);
154                 count = 0;
155                 while (snd_seq_query_next_port(seq, pinfo) >= 0) {
156                         if (check_permission(pinfo, perm)) {
157                                 do_action(seq, cinfo, pinfo, count);
158                                 count++;
159                         }
160                 }
161         }
162 }
163
164
165 static void print_port(snd_seq_t *seq, snd_seq_client_info_t *cinfo,
166                        snd_seq_port_info_t *pinfo, int count)
167 {
168         if (! count) {
169                 printf(_("client %d: '%s' [type=%s]\n"),
170                        snd_seq_client_info_get_client(cinfo),
171                        snd_seq_client_info_get_name(cinfo),
172                        (snd_seq_client_info_get_type(cinfo) == SND_SEQ_USER_CLIENT ?
173                         _("user") : _("kernel")));
174         }
175         printf("  %3d '%-16s'\n",
176                snd_seq_port_info_get_port(pinfo),
177                snd_seq_port_info_get_name(pinfo));
178 }
179
180 static void print_port_and_subs(snd_seq_t *seq, snd_seq_client_info_t *cinfo,
181                                 snd_seq_port_info_t *pinfo, int count)
182 {
183         print_port(seq, cinfo, pinfo, count);
184         list_subscribers(seq, snd_seq_port_info_get_addr(pinfo));
185 }
186
187
188 /*
189  * remove all (exported) connections
190  */
191 static void remove_connection(snd_seq_t *seq, snd_seq_client_info_t *cinfo,
192                               snd_seq_port_info_t *pinfo, int count)
193 {
194         snd_seq_query_subscribe_t *query;
195         snd_seq_port_info_t *port;
196         snd_seq_port_subscribe_t *subs;
197
198         snd_seq_query_subscribe_alloca(&query);
199         snd_seq_query_subscribe_set_root(query, snd_seq_port_info_get_addr(pinfo));
200         snd_seq_query_subscribe_set_type(query, SND_SEQ_QUERY_SUBS_READ);
201         snd_seq_query_subscribe_set_index(query, 0);
202
203         snd_seq_port_info_alloca(&port);
204         snd_seq_port_subscribe_alloca(&subs);
205
206         while (snd_seq_query_port_subscribers(seq, query) >= 0) {
207                 const snd_seq_addr_t *sender = snd_seq_query_subscribe_get_root(query);
208                 const snd_seq_addr_t *dest = snd_seq_query_subscribe_get_addr(query);
209
210                 if (snd_seq_get_any_port_info(seq, dest->client, dest->port, port) < 0 ||
211                     !(snd_seq_port_info_get_capability(port) & SND_SEQ_PORT_CAP_SUBS_WRITE) ||
212                     (snd_seq_port_info_get_capability(port) & SND_SEQ_PORT_CAP_NO_EXPORT)) {
213                         snd_seq_query_subscribe_set_index(query, snd_seq_query_subscribe_get_index(query) + 1);
214                         continue;
215                 }
216                 snd_seq_port_subscribe_set_queue(subs, snd_seq_query_subscribe_get_queue(query));
217                 snd_seq_port_subscribe_set_sender(subs, sender);
218                 snd_seq_port_subscribe_set_dest(subs, dest);
219                 if (snd_seq_unsubscribe_port(seq, subs) < 0) {
220                         snd_seq_query_subscribe_set_index(query, snd_seq_query_subscribe_get_index(query) + 1);
221                 }
222         }
223 }
224
225 static void remove_all_connections(snd_seq_t *seq)
226 {
227         do_search_port(seq, 0, remove_connection);
228 }
229
230
231 /*
232  * main..
233  */
234
235 enum {
236         SUBSCRIBE, UNSUBSCRIBE, LIST, REMOVE_ALL
237 };
238
239 static const struct option long_option[] = {
240         {"disconnect", 0, NULL, 'd'},
241         {"input", 0, NULL, 'i'},
242         {"output", 0, NULL, 'o'},
243         {"real", 1, NULL, 'r'},
244         {"tick", 1, NULL, 't'},
245         {"exclusive", 0, NULL, 'e'},
246         {"list", 0, NULL, 'l'},
247         {"removeall", 0, NULL, 'x'},
248         {NULL, 0, NULL, 0},
249 };
250
251 int main(int argc, char **argv)
252 {
253         int c;
254         snd_seq_t *seq;
255         int queue = 0, convert_time = 0, convert_real = 0, exclusive = 0;
256         int command = SUBSCRIBE;
257         int list_perm = 0;
258         int client;
259         int list_subs = 0;
260         snd_seq_port_subscribe_t *subs;
261         snd_seq_addr_t sender, dest;
262
263 #ifdef ENABLE_NLS
264         setlocale(LC_ALL, "");
265         textdomain(PACKAGE);
266 #endif
267
268         while ((c = getopt_long(argc, argv, "dior:t:elx", long_option, NULL)) != -1) {
269                 switch (c) {
270                 case 'd':
271                         command = UNSUBSCRIBE;
272                         break;
273                 case 'i':
274                         command = LIST;
275                         list_perm |= LIST_INPUT;
276                         break;
277                 case 'o':
278                         command = LIST;
279                         list_perm |= LIST_OUTPUT;
280                         break;
281                 case 'e':
282                         exclusive = 1;
283                         break;
284                 case 'r':
285                         queue = atoi(optarg);
286                         convert_time = 1;
287                         convert_real = 1;
288                         break;
289                 case 't':
290                         queue = atoi(optarg);
291                         convert_time = 1;
292                         convert_real = 0;
293                         break;
294                 case 'l':
295                         command = LIST;
296                         list_subs = 1;
297                         break;
298                 case 'x':
299                         command = REMOVE_ALL;
300                         break;
301                 default:
302                         usage();
303                         exit(1);
304                 }
305         }
306
307         if (snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
308                 fprintf(stderr, _("can't open sequencer\n"));
309                 return 1;
310         }
311         
312         snd_lib_error_set_handler(error_handler);
313
314         switch (command) {
315         case LIST:
316                 do_search_port(seq, list_perm,
317                                list_subs ? print_port_and_subs : print_port);
318                 snd_seq_close(seq);
319                 return 0;
320         case REMOVE_ALL:
321                 remove_all_connections(seq);
322                 snd_seq_close(seq);
323                 return 0;
324         }
325
326         /* connection or disconnection */
327
328         if (optind + 2 > argc) {
329                 snd_seq_close(seq);
330                 usage();
331                 exit(1);
332         }
333
334         if ((client = snd_seq_client_id(seq)) < 0) {
335                 snd_seq_close(seq);
336                 fprintf(stderr, _("can't get client id\n"));
337                 return 1;
338         }
339
340         /* set client info */
341         if (snd_seq_set_client_name(seq, "ALSA Connector") < 0) {
342                 snd_seq_close(seq);
343                 fprintf(stderr, _("can't set client info\n"));
344                 return 1;
345         }
346
347         /* set subscription */
348         if (snd_seq_parse_address(seq, &sender, argv[optind]) < 0) {
349                 snd_seq_close(seq);
350                 fprintf(stderr, _("invalid sender address %s\n"), argv[optind]);
351                 return 1;
352         }
353         if (snd_seq_parse_address(seq, &dest, argv[optind + 1]) < 0) {
354                 snd_seq_close(seq);
355                 fprintf(stderr, _("invalid destination address %s\n"), argv[optind + 1]);
356                 return 1;
357         }
358         snd_seq_port_subscribe_alloca(&subs);
359         snd_seq_port_subscribe_set_sender(subs, &sender);
360         snd_seq_port_subscribe_set_dest(subs, &dest);
361         snd_seq_port_subscribe_set_queue(subs, queue);
362         snd_seq_port_subscribe_set_exclusive(subs, exclusive);
363         snd_seq_port_subscribe_set_time_update(subs, convert_time);
364         snd_seq_port_subscribe_set_time_real(subs, convert_real);
365
366         if (command == UNSUBSCRIBE) {
367                 if (snd_seq_get_port_subscription(seq, subs) < 0) {
368                         snd_seq_close(seq);
369                         fprintf(stderr, _("No subscription is found\n"));
370                         return 1;
371                 }
372                 if (snd_seq_unsubscribe_port(seq, subs) < 0) {
373                         snd_seq_close(seq);
374                         fprintf(stderr, _("Disconnection failed (%s)\n"), snd_strerror(errno));
375                         return 1;
376                 }
377         } else {
378                 if (snd_seq_get_port_subscription(seq, subs) == 0) {
379                         snd_seq_close(seq);
380                         fprintf(stderr, _("Connection is already subscribed\n"));
381                         return 1;
382                 }
383                 if (snd_seq_subscribe_port(seq, subs) < 0) {
384                         snd_seq_close(seq);
385                         fprintf(stderr, _("Connection failed (%s)\n"), snd_strerror(errno));
386                         return 1;
387                 }
388         }
389
390         snd_seq_close(seq);
391
392         return 0;
393 }