1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <syslog.h>
7
8 #include "cras_util.h"
9 #include "cras_volume_curve.h"
10 #include "iniparser_wrapper.h"
11 #include "utlist.h"
12
13 struct cras_card_config {
14 dictionary *ini;
15 };
16
17 static struct cras_volume_curve *
create_simple_step_curve(const struct cras_card_config * card_config,const char * control_name)18 create_simple_step_curve(const struct cras_card_config *card_config,
19 const char *control_name)
20 {
21 char ini_key[MAX_INI_KEY_LENGTH + 1];
22 int max_volume;
23 int volume_step;
24
25 snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:max_volume", control_name);
26 ini_key[MAX_INI_KEY_LENGTH] = 0;
27 max_volume = iniparser_getint(card_config->ini, ini_key, 0);
28 snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:volume_step", control_name);
29 ini_key[MAX_INI_KEY_LENGTH] = 0;
30 volume_step = iniparser_getint(card_config->ini, ini_key, 300);
31 syslog(LOG_INFO, "Configure curve found for %s.", control_name);
32 return cras_volume_curve_create_simple_step(max_volume, volume_step);
33 }
34
35 static struct cras_volume_curve *
create_explicit_curve(const struct cras_card_config * card_config,const char * control_name)36 create_explicit_curve(const struct cras_card_config *card_config,
37 const char *control_name)
38 {
39 unsigned int i;
40 char ini_key[MAX_INI_KEY_LENGTH + 1];
41 long dB_values[101];
42
43 for (i = 0; i < 101; i++) {
44 snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:dB_at_%u",
45 control_name, i);
46 ini_key[MAX_INI_KEY_LENGTH] = 0;
47 dB_values[i] = iniparser_getint(card_config->ini, ini_key, 0);
48 }
49 syslog(LOG_INFO, "Explicit volume curve found for %s.", control_name);
50 return cras_volume_curve_create_explicit(dB_values);
51 }
52
53 /*
54 * Exported interface.
55 */
56
cras_card_config_create(const char * config_path,const char * card_name)57 struct cras_card_config *cras_card_config_create(const char *config_path,
58 const char *card_name)
59 {
60 struct cras_card_config *card_config = NULL;
61 char ini_name[MAX_INI_NAME_LENGTH + 1];
62 dictionary *ini;
63
64 snprintf(ini_name, MAX_INI_NAME_LENGTH, "%s/%s", config_path,
65 card_name);
66 ini_name[MAX_INI_NAME_LENGTH] = '\0';
67 ini = iniparser_load_wrapper(ini_name);
68 if (ini == NULL) {
69 syslog(LOG_DEBUG, "No ini file %s", ini_name);
70 return NULL;
71 }
72
73 card_config = calloc(1, sizeof(*card_config));
74 if (card_config == NULL) {
75 iniparser_freedict(ini);
76 return NULL;
77 }
78
79 card_config->ini = ini;
80 syslog(LOG_DEBUG, "Loaded ini file %s", ini_name);
81 return card_config;
82 }
83
cras_card_config_destroy(struct cras_card_config * card_config)84 void cras_card_config_destroy(struct cras_card_config *card_config)
85 {
86 assert(card_config);
87 iniparser_freedict(card_config->ini);
88 free(card_config);
89 }
90
cras_card_config_get_volume_curve_for_control(const struct cras_card_config * card_config,const char * control_name)91 struct cras_volume_curve *cras_card_config_get_volume_curve_for_control(
92 const struct cras_card_config *card_config, const char *control_name)
93 {
94 char ini_key[MAX_INI_KEY_LENGTH + 1];
95 const char *curve_type;
96
97 if (card_config == NULL || control_name == NULL)
98 return NULL;
99
100 snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:volume_curve", control_name);
101 ini_key[MAX_INI_KEY_LENGTH] = 0;
102 curve_type = iniparser_getstring(card_config->ini, ini_key, NULL);
103
104 if (curve_type && strcmp(curve_type, "simple_step") == 0)
105 return create_simple_step_curve(card_config, control_name);
106 if (curve_type && strcmp(curve_type, "explicit") == 0)
107 return create_explicit_curve(card_config, control_name);
108 syslog(LOG_DEBUG, "No configure curve found for %s.", control_name);
109 return NULL;
110 }
111