c1bab6b1c06e388069c886809a801615262ca00d
[profile/ivi/pulseaudio.git] / src / utils / pabrowse.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
29
30 #include <polyp/mainloop.h>
31 #include <polyp/mainloop-signal.h>
32 #include <polyp/browser.h>
33
34 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
35     fprintf(stderr, "Got signal, exiting\n");
36     m->quit(m, 0);
37 }
38
39 static void dump_server(const pa_browse_info *i) {
40     char t[16];
41
42     if (i->cookie)
43         snprintf(t, sizeof(t), "0x%08x", *i->cookie);
44     
45     printf("server: %s\n"
46            "server-version: %s\n"
47            "user-name: %s\n"
48            "fqdn: %s\n"
49            "cookie: %s\n",
50            i->server,
51            i->server_version ? i->server_version : "n/a",
52            i->user_name ? i->user_name : "n/a",
53            i->fqdn ? i->fqdn : "n/a",
54            i->cookie ? t : "n/a");
55 }
56
57 static void dump_device(const pa_browse_info *i) {
58     char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
59
60     if (i->sample_spec)
61         pa_sample_spec_snprint(ss, sizeof(ss), i->sample_spec);
62
63     printf("device: %s\n"
64            "description: %s\n"
65            "sample spec: %s\n",
66            i->device,
67            i->description ? i->description : "n/a",
68            i->sample_spec ? ss : "n/a");
69            
70 }
71
72 static void browser_callback(pa_browser *b, pa_browse_opcode_t c, const pa_browse_info *i, void *userdata) {
73     assert(b && i);
74
75     switch (c) {
76
77         case PA_BROWSE_NEW_SERVER:
78             printf("\n=> new server <%s>\n", i->name);
79             dump_server(i);
80             break;
81
82         case PA_BROWSE_NEW_SINK:
83             printf("\n=> new sink <%s>\n", i->name);
84             dump_server(i);
85             dump_device(i);
86             break;
87
88         case PA_BROWSE_NEW_SOURCE:
89             printf("\n=> new source <%s>\n", i->name);
90             dump_server(i);
91             dump_device(i);
92             break;
93             
94         case PA_BROWSE_REMOVE_SERVER:
95             printf("\n=> removed server <%s>\n", i->name);
96             break;
97
98         case PA_BROWSE_REMOVE_SINK:
99             printf("\n=> removed sink <%s>\n", i->name);
100             break;
101
102         case PA_BROWSE_REMOVE_SOURCE:
103             printf("\n=> removed source <%s>\n", i->name);
104             break;
105
106         default:
107             ;
108     }
109 }
110
111
112 int main(int argc, char *argv[]) {
113     pa_mainloop *mainloop = NULL;
114     pa_browser *browser = NULL;
115     int ret = 1, r;
116
117     if (!(mainloop = pa_mainloop_new()))
118         goto finish;
119
120     r = pa_signal_init(pa_mainloop_get_api(mainloop));
121     assert(r == 0);
122     pa_signal_new(SIGINT, exit_signal_callback, NULL);
123     pa_signal_new(SIGTERM, exit_signal_callback, NULL);
124     signal(SIGPIPE, SIG_IGN);
125     
126     if (!(browser = pa_browser_new(pa_mainloop_get_api(mainloop))))
127         goto finish;
128
129     pa_browser_set_callback(browser, browser_callback, NULL);
130     
131     ret = 0;
132     pa_mainloop_run(mainloop, &ret);
133
134 finish:
135     if (mainloop) {
136         pa_signal_done();
137         pa_mainloop_free(mainloop);
138     }
139
140     return ret;
141 }