add default timeout for old dbus headers
[ardour.git] / libs / ardouralsautil / devicelist.cc
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <alsa/asoundlib.h>
21 #include "pbd/convert.h"
22 #include "ardouralsautil/devicelist.h"
23
24 using namespace std;
25
26 void
27 ARDOUR::get_alsa_audio_device_names (std::map<std::string, std::string>& devices)
28 {
29         snd_ctl_t *handle;
30         snd_ctl_card_info_t *info;
31         snd_pcm_info_t *pcminfo;
32         snd_ctl_card_info_alloca(&info);
33         snd_pcm_info_alloca(&pcminfo);
34         string devname;
35         int cardnum = -1;
36         int device = -1;
37
38         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
39
40                 devname = "hw:";
41                 devname += PBD::to_string (cardnum, std::dec);
42
43                 if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
44
45                         if (snd_ctl_card_info (handle, info) < 0) {
46                                 continue;
47                         }
48
49                         string card_name = snd_ctl_card_info_get_name (info);
50
51                         /* change devname to use ID, not number */
52
53                         devname = "hw:";
54                         devname += snd_ctl_card_info_get_id (info);
55
56                         while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
57
58                                 /* only detect duplex devices here. more
59                                  * complex arrangements are beyond our scope
60                                  */
61
62                                 snd_pcm_info_set_device (pcminfo, device);
63                                 snd_pcm_info_set_subdevice (pcminfo, 0);
64                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
65
66                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
67                                         continue;
68                                 }
69
70                                 snd_pcm_info_set_device (pcminfo, device);
71                                 snd_pcm_info_set_subdevice (pcminfo, 0);
72                                 snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
73
74                                 if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
75                                         continue;
76                                 }
77                                 devname += ',';
78                                 devname += PBD::to_string (device, std::dec);
79                                 devices.insert (std::make_pair (card_name, devname));
80                         }
81
82                         snd_ctl_close(handle);
83                 }
84         }
85 }
86
87 void
88 ARDOUR::get_alsa_rawmidi_device_names (std::map<std::string, std::string>& devices)
89 {
90         int cardnum = -1;
91         snd_ctl_card_info_t *cinfo;
92         snd_ctl_card_info_alloca (&cinfo);
93         while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
94                 snd_ctl_t *handle;
95                 std::string devname = "hw:";
96                 devname += PBD::to_string (cardnum, std::dec);
97                 if (snd_ctl_open (&handle, devname.c_str (), 0) >= 0 && snd_ctl_card_info (handle, cinfo) >= 0) {
98                         int device = -1;
99                         while (snd_ctl_rawmidi_next_device (handle, &device) >= 0 && device >= 0) {
100                                 snd_rawmidi_info_t *info;
101                                 snd_rawmidi_info_alloca (&info);
102                                 snd_rawmidi_info_set_device (info, device);
103
104                                 int subs_in, subs_out;
105
106                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_INPUT);
107                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
108                                         subs_in = snd_rawmidi_info_get_subdevices_count (info);
109                                 } else {
110                                         subs_in = 0;
111                                 }
112
113                                 snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_OUTPUT);
114                                 if (snd_ctl_rawmidi_info (handle, info) >= 0) {
115                                         subs_out = snd_rawmidi_info_get_subdevices_count (info);
116                                 } else {
117                                         subs_out = 0;
118                                 }
119
120                                 const int subs = subs_in > subs_out ? subs_in : subs_out;
121                                 if (!subs) {
122                                         continue;
123                                 }
124
125                                 for (int sub = 0; sub < subs; ++sub) {
126                                         snd_rawmidi_info_set_stream (info, sub < subs_in ?
127                                                         SND_RAWMIDI_STREAM_INPUT :
128                                                         SND_RAWMIDI_STREAM_OUTPUT);
129
130                                         snd_rawmidi_info_set_subdevice (info, sub);
131                                         if (snd_ctl_rawmidi_info (handle, info) < 0) {
132                                                 continue;
133                                         }
134
135                                         const char *sub_name = snd_rawmidi_info_get_subdevice_name (info);
136                                         if (sub == 0 && sub_name[0] == '\0') {
137                                                 devname = "hw:";
138                                                 devname += snd_ctl_card_info_get_id (cinfo);
139                                                 devname += ",";
140                                                 devname += PBD::to_string (device, std::dec);
141
142                                                 std::string card_name;
143                                                 card_name = snd_rawmidi_info_get_name (info);
144                                                 card_name += " (";
145                                                 if (sub < subs_in) card_name += "I";
146                                                 if (sub < subs_out) card_name += "O";
147                                                 card_name += ")";
148
149                                                 devices.insert (std::make_pair (card_name, devname));
150                                                 break;
151                                         } else {
152                                                 devname = "hw:";
153                                                 devname += snd_ctl_card_info_get_id (cinfo);
154                                                 devname += ",";
155                                                 devname += PBD::to_string (device, std::dec);
156                                                 devname += ",";
157                                                 devname += PBD::to_string (sub, std::dec);
158
159                                                 std::string card_name = sub_name;
160                                                 card_name += " (";
161                                                 if (sub < subs_in) card_name += "I";
162                                                 if (sub < subs_out) card_name += "O";
163                                                 card_name += ")";
164                                                 devices.insert (std::make_pair (card_name, devname));
165                                         }
166                                 }
167                         }
168                         snd_ctl_close (handle);
169                 }
170         }
171 }
172
173 int
174 ARDOUR::card_to_num(const char* device_name)
175 {
176         char* ctl_name;
177         const char * comma;
178         snd_ctl_t* ctl_handle;
179         int i = -1;
180
181         if (strncasecmp(device_name, "plughw:", 7) == 0) {
182                 device_name += 4;
183         }
184         if (!(comma = strchr(device_name, ','))) {
185                 ctl_name = strdup(device_name);
186         } else {
187                 ctl_name = strndup(device_name, comma - device_name);
188         }
189
190         if (snd_ctl_open (&ctl_handle, ctl_name, 0) >= 0) {
191                 snd_ctl_card_info_t *card_info;
192                 snd_ctl_card_info_alloca (&card_info);
193                 if (snd_ctl_card_info(ctl_handle, card_info) >= 0) {
194                         i = snd_ctl_card_info_get_card(card_info);
195                 }
196                 snd_ctl_close(ctl_handle);
197         }
198         free(ctl_name);
199         return i;
200 }