summaryrefslogtreecommitdiff
path: root/cras/src/server/config/cras_card_config.c
blob: ae36565d7e5d2439422936440c8b8cff85d5ebf2 (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
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <syslog.h>

#include "cras_util.h"
#include "cras_volume_curve.h"
#include "iniparser_wrapper.h"
#include "utlist.h"

struct cras_card_config {
	dictionary *ini;
};

static struct cras_volume_curve *
create_simple_step_curve(const struct cras_card_config *card_config,
			 const char *control_name)
{
	char ini_key[MAX_INI_KEY_LENGTH + 1];
	int max_volume;
	int volume_step;

	snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:max_volume", control_name);
	ini_key[MAX_INI_KEY_LENGTH] = 0;
	max_volume = iniparser_getint(card_config->ini, ini_key, 0);
	snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:volume_step", control_name);
	ini_key[MAX_INI_KEY_LENGTH] = 0;
	volume_step = iniparser_getint(card_config->ini, ini_key, 300);
	syslog(LOG_INFO, "Configure curve found for %s.", control_name);
	return cras_volume_curve_create_simple_step(max_volume, volume_step);
}

static struct cras_volume_curve *
create_explicit_curve(const struct cras_card_config *card_config,
		      const char *control_name)
{
	unsigned int i;
	char ini_key[MAX_INI_KEY_LENGTH + 1];
	long dB_values[101];

	for (i = 0; i < 101; i++) {
		snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:dB_at_%u",
			 control_name, i);
		ini_key[MAX_INI_KEY_LENGTH] = 0;
		dB_values[i] = iniparser_getint(card_config->ini, ini_key, 0);
	}
	syslog(LOG_INFO, "Explicit volume curve found for %s.", control_name);
	return cras_volume_curve_create_explicit(dB_values);
}

/*
 * Exported interface.
 */

struct cras_card_config *cras_card_config_create(const char *config_path,
						 const char *card_name)
{
	struct cras_card_config *card_config = NULL;
	char ini_name[MAX_INI_NAME_LENGTH + 1];
	dictionary *ini;

	snprintf(ini_name, MAX_INI_NAME_LENGTH, "%s/%s", config_path,
		 card_name);
	ini_name[MAX_INI_NAME_LENGTH] = '\0';
	ini = iniparser_load_wrapper(ini_name);
	if (ini == NULL) {
		syslog(LOG_DEBUG, "No ini file %s", ini_name);
		return NULL;
	}

	card_config = calloc(1, sizeof(*card_config));
	if (card_config == NULL) {
		iniparser_freedict(ini);
		return NULL;
	}

	card_config->ini = ini;
	syslog(LOG_DEBUG, "Loaded ini file %s", ini_name);
	return card_config;
}

void cras_card_config_destroy(struct cras_card_config *card_config)
{
	assert(card_config);
	iniparser_freedict(card_config->ini);
	free(card_config);
}

struct cras_volume_curve *cras_card_config_get_volume_curve_for_control(
	const struct cras_card_config *card_config, const char *control_name)
{
	char ini_key[MAX_INI_KEY_LENGTH + 1];
	const char *curve_type;

	if (card_config == NULL || control_name == NULL)
		return NULL;

	snprintf(ini_key, MAX_INI_KEY_LENGTH, "%s:volume_curve", control_name);
	ini_key[MAX_INI_KEY_LENGTH] = 0;
	curve_type = iniparser_getstring(card_config->ini, ini_key, NULL);

	if (curve_type && strcmp(curve_type, "simple_step") == 0)
		return create_simple_step_curve(card_config, control_name);
	if (curve_type && strcmp(curve_type, "explicit") == 0)
		return create_explicit_curve(card_config, control_name);
	syslog(LOG_DEBUG, "No configure curve found for %s.", control_name);
	return NULL;
}