make pa_mutex_new() and pa_cond_new() succeed in all cases. Similar behaviour to...
[pulseaudio.git] / src / utils / pabrowse.c
blob450182f5fef7b42207da15ea73f4a4f04a1fcc9d
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 PulseAudio 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.
11 PulseAudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
30 #include <pulse/pulseaudio.h>
31 #include <pulse/browser.h>
33 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
34 fprintf(stderr, "Got signal, exiting\n");
35 m->quit(m, 0);
38 static void dump_server(const pa_browse_info *i) {
39 char t[16];
41 if (i->cookie)
42 snprintf(t, sizeof(t), "0x%08x", *i->cookie);
44 printf("server: %s\n"
45 "server-version: %s\n"
46 "user-name: %s\n"
47 "fqdn: %s\n"
48 "cookie: %s\n",
49 i->server,
50 i->server_version ? i->server_version : "n/a",
51 i->user_name ? i->user_name : "n/a",
52 i->fqdn ? i->fqdn : "n/a",
53 i->cookie ? t : "n/a");
56 static void dump_device(const pa_browse_info *i) {
57 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
59 if (i->sample_spec)
60 pa_sample_spec_snprint(ss, sizeof(ss), i->sample_spec);
62 printf("device: %s\n"
63 "description: %s\n"
64 "sample spec: %s\n",
65 i->device,
66 i->description ? i->description : "n/a",
67 i->sample_spec ? ss : "n/a");
71 static void browser_callback(pa_browser *b, pa_browse_opcode_t c, const pa_browse_info *i, void *userdata) {
72 assert(b && i);
74 switch (c) {
76 case PA_BROWSE_NEW_SERVER:
77 printf("\n=> new server <%s>\n", i->name);
78 dump_server(i);
79 break;
81 case PA_BROWSE_NEW_SINK:
82 printf("\n=> new sink <%s>\n", i->name);
83 dump_server(i);
84 dump_device(i);
85 break;
87 case PA_BROWSE_NEW_SOURCE:
88 printf("\n=> new source <%s>\n", i->name);
89 dump_server(i);
90 dump_device(i);
91 break;
93 case PA_BROWSE_REMOVE_SERVER:
94 printf("\n=> removed server <%s>\n", i->name);
95 break;
97 case PA_BROWSE_REMOVE_SINK:
98 printf("\n=> removed sink <%s>\n", i->name);
99 break;
101 case PA_BROWSE_REMOVE_SOURCE:
102 printf("\n=> removed source <%s>\n", i->name);
103 break;
105 default:
110 static void error_callback(pa_browser *b, const char *s, void *userdata) {
111 pa_mainloop_api*m = userdata;
113 fprintf(stderr, "Failure: %s\n", s);
114 m->quit(m, 1);
117 int main(int argc, char *argv[]) {
118 pa_mainloop *mainloop = NULL;
119 pa_browser *browser = NULL;
120 int ret = 1, r;
121 const char *s;
123 if (!(mainloop = pa_mainloop_new()))
124 goto finish;
126 r = pa_signal_init(pa_mainloop_get_api(mainloop));
127 assert(r == 0);
128 pa_signal_new(SIGINT, exit_signal_callback, NULL);
129 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
130 signal(SIGPIPE, SIG_IGN);
132 if (!(browser = pa_browser_new_full(pa_mainloop_get_api(mainloop), PA_BROWSE_FOR_SERVERS|PA_BROWSE_FOR_SINKS|PA_BROWSE_FOR_SOURCES, &s))) {
133 fprintf(stderr, "pa_browse_new_full(): %s\n", s);
134 goto finish;
137 pa_browser_set_callback(browser, browser_callback, NULL);
138 pa_browser_set_error_callback(browser, error_callback, pa_mainloop_get_api(mainloop));
140 ret = 0;
141 pa_mainloop_run(mainloop, &ret);
143 finish:
145 if (browser)
146 pa_browser_unref(browser);
148 if (mainloop) {
149 pa_signal_done();
150 pa_mainloop_free(mainloop);
153 return ret;