summaryrefslogtreecommitdiffstats
path: root/polyp/cli-text.c
blob: c08b0d9cf3de95d27494128bd3711d8229af7d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <string.h>

#include "cli-text.h"
#include "module.h"
#include "client.h"
#include "sink.h"
#include "source.h"
#include "sink-input.h"
#include "source-output.h"
#include "strbuf.h"
#include "sample-util.h"
#include "scache.h"
#include "autoload.h"

char *pa_module_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_module *m;
    uint32_t index = PA_IDXSET_INVALID;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u module(s) loaded.\n", pa_idxset_ncontents(c->modules));
    
    for (m = pa_idxset_first(c->modules, &index); m; m = pa_idxset_next(c->modules, &index))
        pa_strbuf_printf(s, "    index: %u\n\tname: <%s>\n\targument: <%s>\n\tused: %i\n\tauto unload: %s\n", m->index, m->name, m->argument, m->n_used, m->auto_unload ? "yes" : "no");
    
    return pa_strbuf_tostring_free(s);
}

char *pa_client_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_client *client;
    uint32_t index = PA_IDXSET_INVALID;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u client(s) logged in.\n", pa_idxset_ncontents(c->clients));
    
    for (client = pa_idxset_first(c->clients, &index); client; client = pa_idxset_next(c->clients, &index)) {
        pa_strbuf_printf(s, "    index: %u\n\tname: <%s>\n\tprotocol_name: <%s>\n", client->index, client->name, client->protocol_name);

        if (client->owner)
            pa_strbuf_printf(s, "\towner module: <%u>\n", client->owner->index);
    }
        
    return pa_strbuf_tostring_free(s);
}

char *pa_sink_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_sink *sink;
    uint32_t index = PA_IDXSET_INVALID;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u sink(s) available.\n", pa_idxset_ncontents(c->sinks));

    for (sink = pa_idxset_first(c->sinks, &index); sink; sink = pa_idxset_next(c->sinks, &index)) {
        char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
        pa_sample_spec_snprint(ss, sizeof(ss), &sink->sample_spec);
        assert(sink->monitor_source);
        pa_strbuf_printf(
            s,
            "  %c index: %u\n\tname: <%s>\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tmonitor_source: <%u>\n\tsample_spec: <%s>\n",
            c->default_sink_name && !strcmp(sink->name, c->default_sink_name) ? '*' : ' ',
            sink->index, sink->name,
            (unsigned) sink->volume,
            pa_volume_to_dB(sink->volume),
            (float) pa_sink_get_latency(sink),
            sink->monitor_source->index,
            ss);

        if (sink->owner)
            pa_strbuf_printf(s, "\towner module: <%u>\n", sink->owner->index);
        if (sink->description)
            pa_strbuf_printf(s, "\tdescription: <%s>\n", sink->description);
    }
    
    return pa_strbuf_tostring_free(s);
}

char *pa_source_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_source *source;
    uint32_t index = PA_IDXSET_INVALID;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_ncontents(c->sources));

    for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index)) {
        char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
        pa_sample_spec_snprint(ss, sizeof(ss), &source->sample_spec);
        pa_strbuf_printf(s, "  %c index: %u\n\tname: <%s>\n\tlatency: <%0.0f usec>\n\tsample_spec: <%s>\n",
                         c->default_source_name && !strcmp(source->name, c->default_source_name) ? '*' : ' ',
                         source->index,
                         source->name,
                         (float) pa_source_get_latency(source),
                         ss);

        if (source->monitor_of) 
            pa_strbuf_printf(s, "\tmonitor_of: <%u>\n", source->monitor_of->index);
        if (source->owner)
            pa_strbuf_printf(s, "\towner module: <%u>\n", source->owner->index);
        if (source->description)
            pa_strbuf_printf(s, "\tdescription: <%s>\n", source->description);
    }
    
    return pa_strbuf_tostring_free(s);
}


char *pa_source_output_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_source_output *o;
    uint32_t index = PA_IDXSET_INVALID;
    static const char* const state_table[] = {
        "RUNNING",
        "CORKED",
        "DISCONNECTED"
    };
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u source outputs(s) available.\n", pa_idxset_ncontents(c->source_outputs));

    for (o = pa_idxset_first(c->source_outputs, &index); o; o = pa_idxset_next(c->source_outputs, &index)) {
        char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
        pa_sample_spec_snprint(ss, sizeof(ss), &o->sample_spec);
        assert(o->source);
        pa_strbuf_printf(
            s, "  index: %u\n\tname: '%s'\n\tstate: %s\n\tsource: <%u> '%s'\n\tsample_spec: <%s>\n",
            o->index,
            o->name,
            state_table[o->state],
            o->source->index, o->source->name,
            ss);
        if (o->owner)
            pa_strbuf_printf(s, "\towner module: <%u>\n", o->owner->index);
        if (o->client)
            pa_strbuf_printf(s, "\tclient: <%u> '%s'\n", o->client->index, o->client->name);
    }
    
    return pa_strbuf_tostring_free(s);
}

char *pa_sink_input_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    struct pa_sink_input *i;
    uint32_t index = PA_IDXSET_INVALID;
    static const char* const state_table[] = {
        "RUNNING",
        "CORKED",
        "DISCONNECTED"
    };

    assert(c);
    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_ncontents(c->sink_inputs));

    for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
        char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
        pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
        assert(i->sink);
        pa_strbuf_printf(
            s, "    index: %u\n\tname: <%s>\n\tstate: %s\n\tsink: <%u> '%s'\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tsample_spec: <%s>\n",
            i->index,
            i->name,
            state_table[i->state],
            i->sink->index, i->sink->name,
            (unsigned) i->volume,
            pa_volume_to_dB(i->volume),
            (float) pa_sink_input_get_latency(i),
            ss);

        if (i->owner)
            pa_strbuf_printf(s, "\towner module: <%u>\n", i->owner->index);
        if (i->client)
            pa_strbuf_printf(s, "\tclient: <%u> '%s'\n", i->client->index, i->client->name);
    }
    
    return pa_strbuf_tostring_free(s);
}

char *pa_scache_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u cache entries available.\n", c->scache ? pa_idxset_ncontents(c->scache) : 0);

    if (c->scache) {
        struct pa_scache_entry *e;
        uint32_t index = PA_IDXSET_INVALID;

        for (e = pa_idxset_first(c->scache, &index); e; e = pa_idxset_next(c->scache, &index)) {
            double l = 0;
            char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = "n/a";
            
            if (e->memchunk.memblock) {
                pa_sample_spec_snprint(ss, sizeof(ss), &e->sample_spec);
                l = (double) e->memchunk.length / pa_bytes_per_second(&e->sample_spec);
            }
            
            pa_strbuf_printf(
                s, "    name: <%s>\n\tindex: <%u>\n\tsample_spec: <%s>\n\tlength: <%u>\n\tduration: <%0.1fs>\n\tvolume: <0x%04x>\n\tlazy: %s\n\tfilename: %s\n",
                e->name,
                e->index,
                ss,
                e->memchunk.memblock ? e->memchunk.length : 0,
                l,
                e->volume,
                e->lazy ? "yes" : "no",
                e->filename ? e->filename : "n/a");
        }
    }

    return pa_strbuf_tostring_free(s);
}

char *pa_autoload_list_to_string(struct pa_core *c) {
    struct pa_strbuf *s;
    assert(c);

    s = pa_strbuf_new();
    assert(s);

    pa_strbuf_printf(s, "%u autoload entries available.\n", c->autoload_hashmap ? pa_hashmap_ncontents(c->autoload_hashmap) : 0);

    if (c->autoload_hashmap) {
        struct pa_autoload_entry *e;
        void *state = NULL;

        while ((e = pa_hashmap_iterate(c->autoload_hashmap, &state))) {
            pa_strbuf_printf(
                s, "    name: <%s>\n\ttype: <%s>\n\tindex: <%u>\n\tmodule_name: <%s>\n\targuments: <%s>\n",
                e->name,
                e->type == PA_NAMEREG_SOURCE ? "source" : "sink",
                e->index,
                e->module,
                e->argument);

        }
    }

    return pa_strbuf_tostring_free(s);
}