summaryrefslogtreecommitdiff
path: root/alsactl/info.c
blob: 253539d8c9419305f33bf8f2f2dfa62d40926a02 (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
/*
 *  Advanced Linux Sound Architecture Control Program - General info
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
 *
 *
 *   This program 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.
 *
 *   This program 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "aconfig.h"
#include "alsactl.h"

static int pcm_device_list(snd_ctl_t *ctl, snd_pcm_stream_t stream, bool *first)
{
#ifdef __ALSA_PCM_H
	int err, dev, idx;
	unsigned int count;
	snd_pcm_info_t *pcminfo;
	snd_pcm_info_alloca(&pcminfo);
	bool streamfirst, subfirst;

	dev = -1;
	streamfirst = true;
	while (1) {
		if ((err = snd_ctl_pcm_next_device(ctl, &dev)) < 0) {
			error("snd_ctl_pcm_next_device");
			return err;
		}
		if (dev < 0)
			break;
		snd_pcm_info_set_device(pcminfo, dev);
		snd_pcm_info_set_subdevice(pcminfo, 0);
		snd_pcm_info_set_stream(pcminfo, stream);
		if ((err = snd_ctl_pcm_info(ctl, pcminfo)) < 0) {
			if (err != -ENOENT)
				return err;
			continue;
		}
		if (*first) {
			printf("  pcm:\n");
			*first = false;
		}
		if (streamfirst) {
			printf("    - stream: %s\n      devices:\n", snd_pcm_stream_name(stream));
			streamfirst = false;
		}
		printf("        - device: %d\n          id: %s\n          name: %s\n",
				dev,
				snd_pcm_info_get_id(pcminfo),
				snd_pcm_info_get_name(pcminfo));
		count = snd_pcm_info_get_subdevices_count(pcminfo);
		subfirst = true;
		for (idx = 0; idx < (int)count; idx++) {
			snd_pcm_info_set_subdevice(pcminfo, idx);
			if ((err = snd_ctl_pcm_info(ctl, pcminfo)) < 0) {
				error("control digital audio playback info (%s): %s", snd_ctl_name(ctl), snd_strerror(err));
				return err;
			}
			if (subfirst) {
				printf("          subdevices:\n");
				subfirst = false;
			}
			printf("            - subdevice: %d\n              name: %s\n",
						idx, snd_pcm_info_get_subdevice_name(pcminfo));
		}
	}
#endif
	return 0;
}

static const char *snd_rawmidi_stream_name(snd_rawmidi_stream_t stream)
{
	if (stream == SND_RAWMIDI_STREAM_INPUT)
		return "INPUT";
	if (stream == SND_RAWMIDI_STREAM_OUTPUT)
		return "OUTPUT";
	return "???";
}

static int rawmidi_device_list(snd_ctl_t *ctl, snd_rawmidi_stream_t stream, bool *first)
{
#ifdef __ALSA_RAWMIDI_H
	int err, dev, idx;
	unsigned int count;
	snd_rawmidi_info_t *info;
	snd_rawmidi_info_alloca(&info);
	bool streamfirst, subfirst;

	dev = -1;
	streamfirst = true;
	while (1) {
		if ((err = snd_ctl_rawmidi_next_device(ctl, &dev)) < 0) {
			error("snd_ctl_rawmidi_next_device");
			return err;
		}
		if (dev < 0)
			break;
		snd_rawmidi_info_set_device(info, dev);
		snd_rawmidi_info_set_subdevice(info, 0);
		snd_rawmidi_info_set_stream(info, stream);
		if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0) {
			if (err != -ENOENT)
				return err;
			continue;
		}
		if (*first) {
			printf("  rawmidi:\n");
			*first = false;
		}
		if (streamfirst) {
			printf("    - stream: %s\n      devices:\n", snd_rawmidi_stream_name(stream));
			streamfirst = false;
		}
		printf("        - device: %d\n          id: %s\n          name: %s\n",
				dev,
				snd_rawmidi_info_get_id(info),
				snd_rawmidi_info_get_name(info));
		count = snd_rawmidi_info_get_subdevices_count(info);
		subfirst = true;
		for (idx = 0; idx < (int)count; idx++) {
			snd_rawmidi_info_set_subdevice(info, idx);
			if ((err = snd_ctl_rawmidi_info(ctl, info)) < 0) {
				error("control digital audio playback info (%s): %s", snd_ctl_name(ctl), snd_strerror(err));
				return err;
			}
			if (subfirst) {
				printf("          subdevices:\n");
				subfirst = false;
			}
			printf("            - subdevice: %d\n              name: %s\n",
						idx, snd_rawmidi_info_get_subdevice_name(info));
		}
	}
#endif
	return 0;
}

static int hwdep_device_list(snd_ctl_t *ctl)
{
#ifdef __ALSA_HWDEP_H
	int err, dev;
	snd_hwdep_info_t *info;
	snd_hwdep_info_alloca(&info);
	bool first;

	dev = -1;
	first = true;
	while (1) {
		if ((err = snd_ctl_hwdep_next_device(ctl, &dev)) < 0) {
			error("snd_ctl_pcm_next_device");
			return err;
		}
		if (dev < 0)
			break;
		snd_hwdep_info_set_device(info, dev);
		if ((err = snd_ctl_hwdep_info(ctl, info)) < 0) {
			if (err != -ENOENT)
				return err;
			continue;
		}
		if (first) {
			printf("  hwdep:\n");
			first = false;
		}
		printf("    - device: %d\n      id: %s\n      name: %s\n      iface: %d\n",
				dev,
				snd_hwdep_info_get_id(info),
				snd_hwdep_info_get_name(info),
				snd_hwdep_info_get_iface(info));
	}
#endif
	return 0;
}

static int card_info(snd_ctl_t *ctl)
{
	snd_ctl_card_info_t *info;
	snd_ctl_elem_list_t *clist;
	int err;

	snd_ctl_card_info_alloca(&info);
	snd_ctl_elem_list_alloca(&clist);

	if ((err = snd_ctl_card_info(ctl, info)) < 0) {
		error("Control device %s hw info error: %s", snd_ctl_name(ctl), snd_strerror(err));
		return err;
	}
	printf("#\n# Sound card\n#\n");
	printf("- card: %i\n  id: %s\n  name: %s\n  longname: %s\n",
		snd_ctl_card_info_get_card(info),
		snd_ctl_card_info_get_id(info),
		snd_ctl_card_info_get_name(info),
		snd_ctl_card_info_get_longname(info));
	printf("  mixer_name: %s\n", snd_ctl_card_info_get_mixername(info));
	printf("  components: %s\n", snd_ctl_card_info_get_components(info));
	if ((err = snd_ctl_elem_list(ctl, clist)) < 0) {
		error("snd_ctl_elem_list failure: %s", snd_strerror(err));
	} else {
		printf("  controls_count: %i\n", snd_ctl_elem_list_get_count(clist));
	}
	return err;
}

int general_card_info(int cardno)
{
	snd_ctl_t *ctl;
	char dev[16];
	bool first;
	int err;

	snprintf(dev, sizeof(dev), "hw:%i", cardno);
	if ((err = snd_ctl_open(&ctl, dev, 0)) < 0) {
		error("Control device %s open error: %s", dev, snd_strerror(err));
		return err;
	}
	err = card_info(ctl);

	first = true;
	if (err >= 0)
		err = pcm_device_list(ctl, SND_PCM_STREAM_PLAYBACK, &first);
	if (err >= 0)
		err = pcm_device_list(ctl, SND_PCM_STREAM_CAPTURE, &first);

	first = true;
	if (err >= 0)
		err = rawmidi_device_list(ctl, SND_PCM_STREAM_PLAYBACK, &first);
	if (err >= 0)
		err = rawmidi_device_list(ctl, SND_PCM_STREAM_CAPTURE, &first);

	if (err >= 0)
		err = hwdep_device_list(ctl);
	snd_ctl_close(ctl);
	return err;
}

int general_info(const char *cardname)
{
	struct snd_card_iterator iter;
	int err;

	err = snd_card_iterator_sinit(&iter, cardname);
	if (err < 0)
		return err;
	while (snd_card_iterator_next(&iter)) {
		if ((err = general_card_info(iter.card)))
			return err;
	}
	return snd_card_iterator_error(&iter);
}