Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / cli-text.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <assert.h>
29 #include <string.h>
30
31 #include <pulse/volume.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/module.h>
35 #include <pulsecore/client.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/source-output.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-scache.h>
43 #include <pulsecore/autoload.h>
44
45 #include "cli-text.h"
46
47 char *pa_module_list_to_string(pa_core *c) {
48     pa_strbuf *s;
49     pa_module *m;
50     uint32_t idx = PA_IDXSET_INVALID;
51     assert(c);
52
53     s = pa_strbuf_new();
54     assert(s);
55
56     pa_strbuf_printf(s, "%u module(s) loaded.\n", pa_idxset_size(c->modules));
57
58     for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
59         pa_strbuf_printf(s, "    index: %u\n"
60             "\tname: <%s>\n"
61             "\targument: <%s>\n"
62             "\tused: %i\n"
63             "\tauto unload: %s\n",
64             m->index, m->name, m->argument ? m->argument : "", m->n_used,
65             m->auto_unload ? "yes" : "no");
66     }
67
68     return pa_strbuf_tostring_free(s);
69 }
70
71 char *pa_client_list_to_string(pa_core *c) {
72     pa_strbuf *s;
73     pa_client *client;
74     uint32_t idx = PA_IDXSET_INVALID;
75     assert(c);
76
77     s = pa_strbuf_new();
78     assert(s);
79
80     pa_strbuf_printf(s, "%u client(s) logged in.\n", pa_idxset_size(c->clients));
81
82     for (client = pa_idxset_first(c->clients, &idx); client; client = pa_idxset_next(c->clients, &idx)) {
83         pa_strbuf_printf(s, "    index: %u\n\tname: <%s>\n\tdriver: <%s>\n", client->index, client->name, client->driver);
84
85         if (client->owner)
86             pa_strbuf_printf(s, "\towner module: <%u>\n", client->owner->index);
87     }
88
89     return pa_strbuf_tostring_free(s);
90 }
91
92 char *pa_sink_list_to_string(pa_core *c) {
93     pa_strbuf *s;
94     pa_sink *sink;
95     uint32_t idx = PA_IDXSET_INVALID;
96     static const char* const state_table[] = {
97         [PA_SINK_RUNNING] = "RUNNING",
98         [PA_SINK_SUSPENDED] = "SUSPENDED",
99         [PA_SINK_IDLE] = "IDLE",
100         [PA_SINK_DISCONNECTED] = "DISCONNECTED"
101     };
102     assert(c);
103
104     s = pa_strbuf_new();
105     assert(s);
106
107     pa_strbuf_printf(s, "%u sink(s) available.\n", pa_idxset_size(c->sinks));
108
109     for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx)) {
110         char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
111
112         pa_strbuf_printf(
113             s,
114             "  %c index: %u\n"
115             "\tname: <%s>\n"
116             "\tdriver: <%s>\n"
117             "\tis_hardware: <%i>\n"
118             "\tstate: %s\n"
119             "\tvolume: <%s>\n"
120             "\tmute: <%i>\n"
121             "\tlatency: <%0.0f usec>\n"
122             "\tmonitor_source: <%u>\n"
123             "\tsample spec: <%s>\n"
124             "\tchannel map: <%s>\n",
125             c->default_sink_name && !strcmp(sink->name, c->default_sink_name) ? '*' : ' ',
126             sink->index,
127             sink->name,
128             sink->driver,
129             !!sink->is_hardware,
130             state_table[pa_sink_get_state(sink)],
131             pa_cvolume_snprint(cv, sizeof(cv), pa_sink_get_volume(sink)),
132             !!pa_sink_get_mute(sink),
133             (double) pa_sink_get_latency(sink),
134             sink->monitor_source ? sink->monitor_source->index : PA_INVALID_INDEX,
135             pa_sample_spec_snprint(ss, sizeof(ss), &sink->sample_spec),
136             pa_channel_map_snprint(cm, sizeof(cm), &sink->channel_map));
137
138         if (sink->module)
139             pa_strbuf_printf(s, "\tmodule: <%u>\n", sink->module->index);
140         if (sink->description)
141             pa_strbuf_printf(s, "\tdescription: <%s>\n", sink->description);
142     }
143
144     return pa_strbuf_tostring_free(s);
145 }
146
147 char *pa_source_list_to_string(pa_core *c) {
148     pa_strbuf *s;
149     pa_source *source;
150     uint32_t idx = PA_IDXSET_INVALID;
151     static const char* const state_table[] = {
152         [PA_SOURCE_RUNNING] = "RUNNING",
153         [PA_SOURCE_SUSPENDED] = "SUSPENDED",
154         [PA_SOURCE_IDLE] = "IDLE",
155         [PA_SOURCE_DISCONNECTED] = "DISCONNECTED"
156     };
157     assert(c);
158
159     s = pa_strbuf_new();
160     assert(s);
161
162     pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_size(c->sources));
163
164     for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx)) {
165         char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX];
166
167
168         pa_strbuf_printf(
169             s,
170             "  %c index: %u\n"
171             "\tname: <%s>\n"
172             "\tdriver: <%s>\n"
173             "\tis_hardware: <%i>\n"
174             "\tstate: %s\n"
175             "\tvolume: <%s>\n"
176             "\tmute: <%u>\n"
177             "\tlatency: <%0.0f usec>\n"
178             "\tsample spec: <%s>\n"
179             "\tchannel map: <%s>\n",
180             c->default_source_name && !strcmp(source->name, c->default_source_name) ? '*' : ' ',
181             source->index,
182             source->name,
183             source->driver,
184             !!source->is_hardware,
185             state_table[pa_source_get_state(source)],
186             pa_cvolume_snprint(cv, sizeof(cv), pa_source_get_volume(source)),
187             !!pa_source_get_mute(source),
188             (double) pa_source_get_latency(source),
189             pa_sample_spec_snprint(ss, sizeof(ss), &source->sample_spec),
190             pa_channel_map_snprint(cm, sizeof(cm), &source->channel_map));
191
192         if (source->monitor_of)
193             pa_strbuf_printf(s, "\tmonitor_of: <%u>\n", source->monitor_of->index);
194         if (source->module)
195             pa_strbuf_printf(s, "\tmodule: <%u>\n", source->module->index);
196         if (source->description)
197             pa_strbuf_printf(s, "\tdescription: <%s>\n", source->description);
198     }
199
200     return pa_strbuf_tostring_free(s);
201 }
202
203
204 char *pa_source_output_list_to_string(pa_core *c) {
205     pa_strbuf *s;
206     pa_source_output *o;
207     uint32_t idx = PA_IDXSET_INVALID;
208     static const char* const state_table[] = {
209         [PA_SOURCE_OUTPUT_RUNNING] = "RUNNING",
210         [PA_SOURCE_OUTPUT_CORKED] = "CORKED",
211         [PA_SOURCE_OUTPUT_DISCONNECTED] = "DISCONNECTED"
212     };
213     assert(c);
214
215     s = pa_strbuf_new();
216     assert(s);
217
218     pa_strbuf_printf(s, "%u source outputs(s) available.\n", pa_idxset_size(c->source_outputs));
219
220     for (o = pa_idxset_first(c->source_outputs, &idx); o; o = pa_idxset_next(c->source_outputs, &idx)) {
221         char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
222
223         assert(o->source);
224
225         pa_strbuf_printf(
226             s,
227             "    index: %u\n"
228             "\tname: '%s'\n"
229             "\tdriver: <%s>\n"
230             "\tstate: %s\n"
231             "\tsource: <%u> '%s'\n"
232             "\tlatency: <%0.0f usec>\n"
233             "\tsample spec: <%s>\n"
234             "\tchannel map: <%s>\n"
235             "\tresample method: %s\n",
236             o->index,
237             o->name,
238             o->driver,
239             state_table[pa_source_output_get_state(o)],
240             o->source->index, o->source->name,
241             (double) pa_source_output_get_latency(o),
242             pa_sample_spec_snprint(ss, sizeof(ss), &o->sample_spec),
243             pa_channel_map_snprint(cm, sizeof(cm), &o->channel_map),
244             pa_resample_method_to_string(pa_source_output_get_resample_method(o)));
245         if (o->module)
246             pa_strbuf_printf(s, "\towner module: <%u>\n", o->module->index);
247         if (o->client)
248             pa_strbuf_printf(s, "\tclient: <%u> '%s'\n", o->client->index, o->client->name);
249     }
250
251     return pa_strbuf_tostring_free(s);
252 }
253
254 char *pa_sink_input_list_to_string(pa_core *c) {
255     pa_strbuf *s;
256     pa_sink_input *i;
257     uint32_t idx = PA_IDXSET_INVALID;
258     static const char* const state_table[] = {
259         [PA_SINK_INPUT_RUNNING] = "RUNNING",
260         [PA_SINK_INPUT_DRAINED] = "DRAINED",
261         [PA_SINK_INPUT_CORKED] = "CORKED",
262         [PA_SINK_INPUT_DISCONNECTED] = "DISCONNECTED"
263     };
264
265     assert(c);
266     s = pa_strbuf_new();
267     assert(s);
268
269     pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_size(c->sink_inputs));
270
271     for (i = pa_idxset_first(c->sink_inputs, &idx); i; i = pa_idxset_next(c->sink_inputs, &idx)) {
272         char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
273
274         assert(i->sink);
275
276         pa_strbuf_printf(
277             s,
278             "    index: %u\n"
279             "\tname: <%s>\n"
280             "\tdriver: <%s>\n"
281             "\tstate: %s\n"
282             "\tsink: <%u> '%s'\n"
283             "\tvolume: <%s>\n"
284             "\tmute: <%i>\n"
285             "\tlatency: <%0.0f usec>\n"
286             "\tsample spec: <%s>\n"
287             "\tchannel map: <%s>\n"
288             "\tresample method: %s\n",
289             i->index,
290             i->name,
291             i->driver,
292             state_table[pa_sink_input_get_state(i)],
293             i->sink->index, i->sink->name,
294             pa_cvolume_snprint(cv, sizeof(cv), pa_sink_input_get_volume(i)),
295             !!pa_sink_input_get_mute(i),
296             (double) pa_sink_input_get_latency(i),
297             pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec),
298             pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
299             pa_resample_method_to_string(pa_sink_input_get_resample_method(i)));
300
301         if (i->module)
302             pa_strbuf_printf(s, "\tmodule: <%u>\n", i->module->index);
303         if (i->client)
304             pa_strbuf_printf(s, "\tclient: <%u> '%s'\n", i->client->index, i->client->name);
305     }
306
307     return pa_strbuf_tostring_free(s);
308 }
309
310 char *pa_scache_list_to_string(pa_core *c) {
311     pa_strbuf *s;
312     assert(c);
313
314     s = pa_strbuf_new();
315     assert(s);
316
317     pa_strbuf_printf(s, "%u cache entries available.\n", c->scache ? pa_idxset_size(c->scache) : 0);
318
319     if (c->scache) {
320         pa_scache_entry *e;
321         uint32_t idx = PA_IDXSET_INVALID;
322
323         for (e = pa_idxset_first(c->scache, &idx); e; e = pa_idxset_next(c->scache, &idx)) {
324             double l = 0;
325             char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = "n/a", cv[PA_CVOLUME_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX] = "n/a";
326
327             if (e->memchunk.memblock) {
328                 pa_sample_spec_snprint(ss, sizeof(ss), &e->sample_spec);
329                 pa_channel_map_snprint(cm, sizeof(cm), &e->channel_map);
330                 l = (double) e->memchunk.length / pa_bytes_per_second(&e->sample_spec);
331             }
332
333             pa_strbuf_printf(
334                 s,
335                 "    name: <%s>\n"
336                 "\tindex: <%u>\n"
337                 "\tsample spec: <%s>\n"
338                 "\tchannel map: <%s>\n"
339                 "\tlength: <%lu>\n"
340                 "\tduration: <%0.1fs>\n"
341                 "\tvolume: <%s>\n"
342                 "\tlazy: %s\n"
343                 "\tfilename: %s\n",
344                 e->name,
345                 e->index,
346                 ss,
347                 cm,
348                 (long unsigned)(e->memchunk.memblock ? e->memchunk.length : 0),
349                 l,
350                 pa_cvolume_snprint(cv, sizeof(cv), &e->volume),
351                 e->lazy ? "yes" : "no",
352                 e->filename ? e->filename : "n/a");
353         }
354     }
355
356     return pa_strbuf_tostring_free(s);
357 }
358
359 char *pa_autoload_list_to_string(pa_core *c) {
360     pa_strbuf *s;
361     assert(c);
362
363     s = pa_strbuf_new();
364     assert(s);
365
366     pa_strbuf_printf(s, "%u autoload entries available.\n", c->autoload_hashmap ? pa_hashmap_size(c->autoload_hashmap) : 0);
367
368     if (c->autoload_hashmap) {
369         pa_autoload_entry *e;
370         void *state = NULL;
371
372         while ((e = pa_hashmap_iterate(c->autoload_hashmap, &state, NULL))) {
373             pa_strbuf_printf(
374                 s, "    name: <%s>\n\ttype: <%s>\n\tindex: <%u>\n\tmodule_name: <%s>\n\targuments: <%s>\n",
375                 e->name,
376                 e->type == PA_NAMEREG_SOURCE ? "source" : "sink",
377                 e->index,
378                 e->module,
379                 e->argument ? e->argument : "");
380
381         }
382     }
383
384     return pa_strbuf_tostring_free(s);
385 }
386
387 char *pa_full_status_string(pa_core *c) {
388     pa_strbuf *s;
389     int i;
390
391     s = pa_strbuf_new();
392
393     for (i = 0; i < 8; i++) {
394         char *t = NULL;
395
396         switch (i) {
397             case 0:
398                 t = pa_sink_list_to_string(c);
399                 break;
400             case 1:
401                 t = pa_source_list_to_string(c);
402                 break;
403             case 2:
404                 t = pa_sink_input_list_to_string(c);
405                 break;
406             case 3:
407                 t = pa_source_output_list_to_string(c);
408                 break;
409             case 4:
410                 t = pa_client_list_to_string(c);
411                 break;
412             case 5:
413                 t = pa_module_list_to_string(c);
414                 break;
415             case 6:
416                 t = pa_scache_list_to_string(c);
417                 break;
418             case 7:
419                 t = pa_autoload_list_to_string(c);
420                 break;
421         }
422
423         pa_strbuf_puts(s, t);
424         pa_xfree(t);
425     }
426
427     return pa_strbuf_tostring_free(s);
428 }