1 // SPDX-License-Identifier: GPL-2.0
3 // subcmd-list.c - operations for list sub command.
5 // Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 // Licensed under the terms of the GNU General Public License, version 2.
21 static int dump_device(snd_ctl_t *handle, const char *id, const char *name,
22 snd_pcm_stream_t direction, snd_pcm_info_t *info)
28 printf("card %i: %s [%s], device %i: %s [%s]\n",
29 snd_pcm_info_get_card(info), id, name,
30 snd_pcm_info_get_device(info), snd_pcm_info_get_id(info),
31 snd_pcm_info_get_name(info));
33 count = snd_pcm_info_get_subdevices_count(info);
34 printf(" Subdevices: %i/%i\n",
35 snd_pcm_info_get_subdevices_avail(info), count);
37 for (i = 0; i < count; ++i) {
38 snd_pcm_info_set_subdevice(info, i);
40 err = snd_ctl_pcm_info(handle, info);
42 printf("control digital audio playback info (%i): %s",
43 snd_pcm_info_get_card(info), snd_strerror(err));
47 printf(" Subdevice #%i: %s\n",
48 i, snd_pcm_info_get_subdevice_name(info));
54 static int dump_devices(snd_ctl_t *handle, const char *id, const char *name,
55 snd_pcm_stream_t direction)
61 err = snd_pcm_info_malloc(&info);
66 err = snd_ctl_pcm_next_device(handle, &device);
72 snd_pcm_info_set_device(info, device);
73 snd_pcm_info_set_subdevice(info, 0);
74 snd_pcm_info_set_stream(info, direction);
75 err = snd_ctl_pcm_info(handle, info);
79 err = dump_device(handle, id, name, direction, info);
88 static int list_devices(snd_pcm_stream_t direction)
93 snd_ctl_card_info_t *info;
96 err = snd_ctl_card_info_malloc(&info);
101 if (snd_card_next(&card) < 0 || card < 0)
104 printf("**** List of %s Hardware Devices ****\n",
105 snd_pcm_stream_name(direction));
108 sprintf(name, "hw:%d", card);
109 err = snd_ctl_open(&handle, name, 0);
111 printf("control open (%i): %s",
112 card, snd_strerror(err));
114 err = snd_ctl_card_info(handle, info);
116 printf("control hardware info (%i): %s",
117 card, snd_strerror(err));
119 err = dump_devices(handle,
120 snd_ctl_card_info_get_id(info),
121 snd_ctl_card_info_get_name(info),
124 snd_ctl_close(handle);
131 if (snd_card_next(&card) < 0) {
132 printf("snd_card_next");
141 static int list_pcms(snd_pcm_stream_t direction)
143 static const char *const filters[] = {
144 [SND_PCM_STREAM_CAPTURE] = "Input",
145 [SND_PCM_STREAM_PLAYBACK] = "Output",
154 if (snd_device_name_hint(-1, "pcm", &hints) < 0)
157 filter = filters[direction];
160 for (n = hints; *n != NULL; ++n) {
161 io = snd_device_name_get_hint(*n, "IOID");
162 if (io != NULL && strcmp(io, filter) != 0) {
167 name = snd_device_name_get_hint(*n, "NAME");
168 desc = snd_device_name_get_hint(*n, "DESC");
170 printf("%s\n", name);
189 snd_device_name_free_hint(hints);
194 static void print_help(void)
196 printf("help for list sub-command.\n");
199 // Backward compatibility to aplay(1).
200 static bool decide_operation(int argc, char *const *argv, enum list_op *op)
202 static const char *s_opts = "hlL";
203 static const struct option l_opts[] = {
204 {"list-devices", 0, NULL, 'l'},
205 {"list-pcms", 0, NULL, 'L'},
212 int c = getopt_long(argc, argv, s_opts, l_opts, NULL);
216 *op = LIST_OP_DEVICE;
228 static int detect_operation(int argc, char *const *argv, enum list_op *op)
230 static const char *const ops[] = {
231 [LIST_OP_DEVICE] = "device",
232 [LIST_OP_PCM] = "pcm",
236 if (strcmp(argv[1], "list") || argc < 3)
239 for (i = 0; i < ARRAY_SIZE(ops); ++i) {
240 if (!strcmp(argv[2], ops[i])) {
249 int subcmd_list(int argc, char *const *argv, snd_pcm_stream_t direction)
251 enum list_op op = LIST_OP_HELP;
254 // Renewed command system.
256 if (!detect_operation(argc, argv, &op) &&
257 !decide_operation(argc, argv, &op))
261 if (op == LIST_OP_DEVICE)
262 err = list_devices(direction);
263 else if (op == LIST_OP_PCM)
264 err = list_pcms(direction);