1 /*****************************************************************
2 * gmerlin - a general purpose multimedia framework and applications
3 *
4 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
5 * gmerlin-general@lists.sourceforge.net
6 * http://gmerlin.sourceforge.net
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * *****************************************************************/
21
22 #include <config.h>
23 #include <gmerlin/translation.h>
24
25 #include <gmerlin/utils.h>
26 #include <gmerlin/log.h>
27 #define LOG_DOMAIN "card"
28
29
30 #include "alsamixer.h"
31
alsa_mixer_control_read(alsa_mixer_control_t * c)32 int alsa_mixer_control_read(alsa_mixer_control_t * c)
33 {
34 if(snd_hctl_elem_read(c->hctl, c->val))
35 {
36 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_hctl_elem_read failed");
37 return 0;
38 }
39 return 1;
40 }
41
42
alsa_mixer_control_write(alsa_mixer_control_t * c)43 int alsa_mixer_control_write(alsa_mixer_control_t * c)
44 {
45 if(snd_hctl_elem_write(c->hctl, c->val))
46 {
47 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_hctl_elem_write failed");
48 return 0;
49 }
50 return 1;
51 }
52
get_group(alsa_card_t * c,const char * label)53 static alsa_mixer_group_t * get_group(alsa_card_t * c, const char * label)
54 {
55 int i;
56 for(i = 0; i < c->num_groups; i++)
57 {
58 if(!strcmp(c->groups[i].label, label))
59 {
60 return &c->groups[i];
61 }
62 }
63 c->num_groups++;
64 c->groups = realloc(c->groups, c->num_groups * sizeof(*(c->groups)));
65 memset(c->groups + (c->num_groups-1), 0, sizeof(*(c->groups)));
66 c->groups[c->num_groups-1].label =
67 bg_strdup(c->groups[c->num_groups-1].label, label);
68 return &c->groups[c->num_groups-1];
69 }
70
create_control(snd_hctl_elem_t * hctl_elem)71 static alsa_mixer_control_t * create_control(snd_hctl_elem_t * hctl_elem)
72 {
73 alsa_mixer_control_t * ret;
74 ret = calloc(1, sizeof(*ret));
75 ret->hctl = hctl_elem;
76 snd_ctl_elem_value_malloc(&ret->val);
77 alsa_mixer_control_read(ret);
78 return ret;
79 }
80
alsa_card_create(int index)81 alsa_card_t * alsa_card_create(int index)
82 {
83 char name[32];
84 alsa_card_t * card;
85 int err;
86 snd_hctl_elem_t * hctl_elem;
87 snd_ctl_t * ctl;
88 const char * element_name;
89 snd_ctl_elem_info_t * info;
90 snd_ctl_elem_id_t * id;
91 snd_ctl_card_info_t * card_info;
92
93 char ** strings;
94 int num_strings;
95 char * label;
96 char * tmp_label;
97 int done;
98 // int is_bass = 0;
99 // int is_treble = 0;
100 // int is_tone_switch;
101
102
103 int is_switch = 0;
104 int is_volume = 0;
105 int is_playback = 0;
106 int is_capture = 0;
107 int elem_index;
108 int i;
109 alsa_mixer_group_t * group;
110
111 sprintf(name, "hw:%d", index);
112
113 card = calloc(1, sizeof(*card));
114
115 if((err = snd_hctl_open(&card->hctl, name, 0)))
116 {
117 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_hctl_open failed");
118 goto fail;
119 }
120 if((err = snd_hctl_load(card->hctl)) < 0)
121 {
122 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_hctl_load failed");
123 goto fail;
124 }
125
126 ctl = snd_hctl_ctl(card->hctl);
127
128 if(snd_ctl_card_info_malloc(&card_info))
129 {
130 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_ctl_card_info_malloc failed");
131 goto fail;
132 }
133 if(snd_ctl_card_info(ctl, card_info))
134 {
135 bg_log(BG_LOG_ERROR, LOG_DOMAIN, "snd_ctl_card_info failed");
136 goto fail;
137 }
138 card->name = bg_strdup(card->name,
139 snd_ctl_card_info_get_mixername(card_info));
140
141 hctl_elem = snd_hctl_first_elem(card->hctl);
142
143 while(hctl_elem)
144 {
145 done = 0;
146 snd_ctl_elem_info_malloc(&info);
147
148 snd_ctl_elem_id_malloc(&id);
149 snd_hctl_elem_get_id(hctl_elem, id);
150 // dump_ctl_elem_id(id);
151 element_name = snd_ctl_elem_id_get_name(id);
152 elem_index = snd_ctl_elem_id_get_index(id);
153
154 /* Special case: Tone control */
155
156 if(!strcmp(element_name, "Tone Control - Switch"))
157 {
158 group = get_group(card, "Tone");
159 group->tone_switch = create_control(hctl_elem);
160 done = 1;
161 }
162 else if(!strcmp(element_name, "Tone Control - Bass"))
163 {
164 group = get_group(card, "Tone");
165 group->tone_bass = create_control(hctl_elem);
166 done = 1;
167 }
168 else if(!strcmp(element_name, "Tone Control - Treble"))
169 {
170 group = get_group(card, "Tone");
171 group->tone_treble = create_control(hctl_elem);
172 done = 1;
173 }
174
175 if(done)
176 {
177 snd_ctl_elem_id_free(id);
178 hctl_elem = snd_hctl_elem_next(hctl_elem);
179 continue;
180 }
181
182 strings = bg_strbreak(element_name, ' ');
183 num_strings = 0;
184 while(strings[num_strings])
185 num_strings++;
186
187 is_switch = 0;
188 is_volume = 0;
189 is_playback = 0;
190 is_capture = 0;
191
192 if(num_strings >= 2)
193 {
194 if(!strcmp(strings[num_strings-1], "Switch"))
195 {
196 is_switch = 1;
197 num_strings--;
198 }
199 else if(!strcmp(strings[num_strings-1], "Volume"))
200 {
201 is_volume = 1;
202 num_strings--;
203 }
204
205 if(!strcmp(strings[num_strings-1], "Playback"))
206 {
207 is_playback = 1;
208 num_strings--;
209 }
210 else if(!strcmp(strings[num_strings-1], "Capture"))
211 {
212 is_capture = 1;
213 num_strings--;
214 }
215 }
216
217 if(num_strings)
218 {
219 label = NULL;
220 for(i = 0; i < num_strings; i++)
221 {
222 label = bg_strcat(label, strings[i]);
223 if(i < num_strings - 1)
224 {
225 label = bg_strcat(label, " ");
226 }
227 }
228 }
229 else if(is_capture && (is_volume || is_switch))
230 {
231 label = bg_strdup(NULL, TR("Capture"));
232 }
233 else
234 {
235 label = bg_strdup(NULL, TR("Unknown"));
236 }
237
238 if(elem_index > 0)
239 {
240 tmp_label = bg_sprintf("%s %d", label, elem_index+1);
241 free(label);
242 label = tmp_label;
243 }
244 group = get_group(card, label);
245
246 if(is_capture)
247 {
248 if(is_switch)
249 group->capture_switch = create_control(hctl_elem);
250 else if(is_volume)
251 group->capture_volume = create_control(hctl_elem);
252 else
253 group->ctl = create_control(hctl_elem);
254 }
255 else if(is_playback)
256 {
257 if(is_switch)
258 group->playback_switch = create_control(hctl_elem);
259 else if(is_volume)
260 group->playback_volume = create_control(hctl_elem);
261 else
262 group->ctl = create_control(hctl_elem);
263 }
264 else
265 group->ctl = create_control(hctl_elem);
266
267 free(label);
268 bg_strbreak_free(strings);
269 snd_ctl_elem_id_free(id);
270 hctl_elem = snd_hctl_elem_next(hctl_elem);
271 }
272
273
274 return card;
275 fail:
276 alsa_card_destroy(card);
277 return NULL;
278 }
279
alsa_card_destroy(alsa_card_t * c)280 void alsa_card_destroy(alsa_card_t * c)
281 {
282 free(c);
283 }
284
dump_ctl_elem_id(snd_ctl_elem_id_t * id)285 static void dump_ctl_elem_id(snd_ctl_elem_id_t * id)
286 {
287 FILE * out = stderr;
288 fprintf(out, " ID:\n");
289 fprintf(out, " numid: %d\n", snd_ctl_elem_id_get_numid(id));
290 fprintf(out, " device: %d\n", snd_ctl_elem_id_get_device(id));
291 fprintf(out, " subdevice: %d\n", snd_ctl_elem_id_get_subdevice(id));
292 fprintf(out, " .name = %s\n", snd_ctl_elem_id_get_name(id));
293 fprintf(out, " index: %d\n", snd_ctl_elem_id_get_index(id));
294 fprintf(out, " interface: %s\n",
295 snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(id)));
296 }
297
dump_ctl_elem_info(snd_hctl_elem_t * hctl,snd_ctl_elem_info_t * info)298 static void dump_ctl_elem_info(snd_hctl_elem_t * hctl,
299 snd_ctl_elem_info_t * info)
300 {
301 FILE * out = stderr;
302 snd_ctl_elem_type_t type;
303 int i, num_items;
304 fprintf(out, " ELEM_INFO:\n");
305
306 type = snd_ctl_elem_info_get_type(info);
307
308 fprintf(out, " Type: %s\n",
309 snd_ctl_elem_type_name(type));
310 fprintf(out, " Owner: %d\n", snd_ctl_elem_info_get_owner(info));
311 fprintf(out, " Count: %d\n", snd_ctl_elem_info_get_count(info));
312 // fprintf(out, " : %d\n", snd_ctl_elem_info_get_count(info));
313
314 if(type == SND_CTL_ELEM_TYPE_INTEGER)
315 {
316 fprintf(out, " Min: %ld, Max: %ld, Step: %ld\n",
317 snd_ctl_elem_info_get_min(info),
318 snd_ctl_elem_info_get_max(info),
319 snd_ctl_elem_info_get_step(info));
320 }
321 else if(type == SND_CTL_ELEM_TYPE_INTEGER64)
322 {
323 fprintf(out, " Min: %lld, Max: %lld, Step: %lld\n",
324 snd_ctl_elem_info_get_min64(info),
325 snd_ctl_elem_info_get_max64(info),
326 snd_ctl_elem_info_get_step64(info));
327 }
328 else if(type == SND_CTL_ELEM_TYPE_ENUMERATED)
329 {
330 num_items = snd_ctl_elem_info_get_items(info);
331 for(i = 0; i < num_items; i++)
332 {
333 snd_ctl_elem_info_set_item(info,i);
334 snd_hctl_elem_info(hctl,info);
335 fprintf(out, " Item %d: %s\n", i+1,
336 snd_ctl_elem_info_get_item_name(info));
337 }
338 }
339 }
340
dump_hctl_elem(snd_hctl_elem_t * h)341 static void dump_hctl_elem(snd_hctl_elem_t * h)
342 {
343 snd_ctl_elem_id_t * id;
344 snd_ctl_elem_info_t * info;
345
346 snd_ctl_elem_id_malloc(&id);
347 snd_hctl_elem_get_id(h, id);
348 dump_ctl_elem_id(id);
349 snd_ctl_elem_id_free(id);
350
351 snd_ctl_elem_info_malloc(&info);
352 snd_hctl_elem_info(h, info);
353 dump_ctl_elem_info(h, info);
354 snd_ctl_elem_info_free(info);
355 }
356
dump_control(alsa_mixer_control_t * c)357 static void dump_control(alsa_mixer_control_t * c)
358 {
359 FILE * out = stderr;
360 fprintf(out, "HCTL:\n");
361 dump_hctl_elem(c->hctl);
362 }
363
dump_group(alsa_mixer_group_t * g)364 static void dump_group(alsa_mixer_group_t * g)
365 {
366 FILE * out = stderr;
367 if(g->playback_switch)
368 {
369 fprintf(out, "Playback switch:");
370 dump_control(g->playback_switch);
371 }
372 if(g->playback_volume)
373 {
374 fprintf(out, "Playback volume:\n");
375 dump_control(g->playback_volume);
376 }
377 if(g->capture_switch)
378 {
379 fprintf(out, "Capture switch:\n");
380 dump_control(g->capture_switch);
381 }
382 if(g->capture_volume)
383 {
384 fprintf(out, "Capture volume:\n");
385 dump_control(g->capture_volume);
386 }
387 if(g->ctl)
388 {
389 fprintf(out, "Control:\n");
390 dump_control(g->ctl);
391 }
392 }
393
alsa_card_dump(alsa_card_t * c)394 void alsa_card_dump(alsa_card_t * c)
395 {
396 int i;
397 FILE * out = stderr;
398 for(i = 0; i < c->num_groups; i++)
399 {
400 fprintf(out, "Group %d: %s\n", i+1, c->groups[i].label);
401 dump_group(&c->groups[i]);
402 }
403
404 }
405