2015-08-04 17:23:03 +02:00
|
|
|
/*
|
|
|
|
Copyright(c) 2014-2015 Intel Corporation
|
|
|
|
Copyright(c) 2010-2011 Texas Instruments Incorporated,
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of version 2 of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
The full GNU General Public License is included in this distribution
|
|
|
|
in the file called LICENSE.GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#include <alsa/topology.h>
|
|
|
|
#include "gettext.h"
|
|
|
|
|
|
|
|
static snd_output_t *log;
|
|
|
|
|
|
|
|
static void usage(char *name)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
_("Usage: %s [OPTIONS]...\n"
|
|
|
|
"\n"
|
|
|
|
"-h, --help help\n"
|
|
|
|
"-c, --compile=FILE compile file\n"
|
2019-12-12 19:00:42 +01:00
|
|
|
"-n, --normalize=FILE normalize file\n"
|
2015-08-04 17:23:03 +02:00
|
|
|
"-v, --verbose=LEVEL set verbosity level (0...1)\n"
|
|
|
|
"-o, --output=FILE set output file\n"
|
2019-12-12 19:18:28 +01:00
|
|
|
"-s, --sort sort the identifiers in the normalized output\n"
|
2015-08-04 17:23:03 +02:00
|
|
|
), name);
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:00:42 +01:00
|
|
|
static int _compar(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const snd_config_t *c1 = *(snd_config_t **)a;
|
|
|
|
const snd_config_t *c2 = *(snd_config_t **)b;
|
|
|
|
const char *id1, *id2;
|
|
|
|
if (snd_config_get_id(c1, &id1)) return 0;
|
|
|
|
if (snd_config_get_id(c2, &id2)) return 0;
|
|
|
|
return strcmp(id1, id2);
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:18:28 +01:00
|
|
|
static snd_config_t *normalize_config(const char *id, snd_config_t *src, int sort)
|
2019-12-12 19:00:42 +01:00
|
|
|
{
|
|
|
|
snd_config_t *dst, **a;
|
|
|
|
snd_config_iterator_t i, next;
|
|
|
|
int index, count;
|
|
|
|
|
|
|
|
if (snd_config_get_type(src) != SND_CONFIG_TYPE_COMPOUND) {
|
|
|
|
if (snd_config_copy(&dst, src) >= 0)
|
|
|
|
return dst;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
snd_config_for_each(i, next, src)
|
|
|
|
count++;
|
|
|
|
a = malloc(sizeof(dst) * count);
|
|
|
|
if (a == NULL)
|
|
|
|
return NULL;
|
|
|
|
index = 0;
|
|
|
|
snd_config_for_each(i, next, src) {
|
|
|
|
snd_config_t *s = snd_config_iterator_entry(i);
|
|
|
|
a[index++] = s;
|
|
|
|
}
|
2019-12-12 19:18:28 +01:00
|
|
|
if (sort)
|
|
|
|
qsort(a, count, sizeof(a[0]), _compar);
|
|
|
|
if (snd_config_make_compound(&dst, id, count == 1)) {
|
|
|
|
free(a);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-12 19:00:42 +01:00
|
|
|
for (index = 0; index < count; index++) {
|
|
|
|
snd_config_t *s = a[index];
|
|
|
|
const char *id2;
|
|
|
|
if (snd_config_get_id(s, &id2)) {
|
|
|
|
snd_config_delete(dst);
|
2019-12-12 19:18:28 +01:00
|
|
|
free(a);
|
2019-12-12 19:00:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-12 19:18:28 +01:00
|
|
|
s = normalize_config(id2, s, sort);
|
2019-12-12 19:00:42 +01:00
|
|
|
if (s == NULL || snd_config_add(dst, s)) {
|
2019-12-13 10:44:36 +01:00
|
|
|
if (s)
|
|
|
|
snd_config_delete(s);
|
2019-12-12 19:00:42 +01:00
|
|
|
snd_config_delete(dst);
|
2019-12-12 19:18:28 +01:00
|
|
|
free(a);
|
2019-12-12 19:00:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 19:18:28 +01:00
|
|
|
free(a);
|
2019-12-12 19:00:42 +01:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compile(const char *source_file, const char *output_file, int verbose)
|
2015-08-04 17:23:03 +02:00
|
|
|
{
|
|
|
|
snd_tplg_t *snd_tplg;
|
2019-12-12 19:00:42 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
snd_tplg = snd_tplg_new();
|
|
|
|
if (snd_tplg == NULL) {
|
|
|
|
fprintf(stderr, _("failed to create new topology context\n"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_tplg_verbose(snd_tplg, verbose);
|
|
|
|
|
|
|
|
err = snd_tplg_build_file(snd_tplg, source_file, output_file);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, _("failed to compile context %s\n"), source_file);
|
|
|
|
snd_tplg_free(snd_tplg);
|
|
|
|
unlink(output_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_tplg_free(snd_tplg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:18:28 +01:00
|
|
|
static int normalize(const char *source_file, const char *output_file, int sort)
|
2019-12-12 19:00:42 +01:00
|
|
|
{
|
|
|
|
snd_input_t *input;
|
|
|
|
snd_output_t *output;
|
|
|
|
snd_config_t *top, *norm;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = snd_input_stdio_open(&input, source_file, "r");
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "Unable to open source file '%s': %s\n", source_file, snd_strerror(-err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_config_top(&top);
|
|
|
|
if (err < 0) {
|
|
|
|
snd_input_close(input);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_config_load(top, input);
|
|
|
|
snd_input_close(input);
|
|
|
|
if (err < 0) {
|
|
|
|
snd_config_delete(top);
|
|
|
|
fprintf(stderr, "Unable to parse source file '%s': %s\n", source_file, snd_strerror(-err));
|
|
|
|
snd_config_delete(top);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_output_stdio_open(&output, output_file, "w+");
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "Unable to open output file '%s': %s\n", output_file, snd_strerror(-err));
|
|
|
|
snd_config_delete(top);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:18:28 +01:00
|
|
|
norm = normalize_config(NULL, top, sort);
|
2019-12-12 19:00:42 +01:00
|
|
|
if (norm == NULL) {
|
|
|
|
fprintf(stderr, "Unable to normalize configuration (out of memory?)\n");
|
|
|
|
snd_output_close(output);
|
|
|
|
snd_config_delete(top);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_config_save(norm, output);
|
|
|
|
snd_output_close(output);
|
2019-12-12 19:18:28 +01:00
|
|
|
snd_config_delete(norm);
|
2019-12-12 19:00:42 +01:00
|
|
|
snd_config_delete(top);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "Unable to save normalized contents: %s\n", snd_strerror(-err));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-12-12 19:18:28 +01:00
|
|
|
static const char short_options[] = "hc:n:v:o:s";
|
2015-08-04 17:23:03 +02:00
|
|
|
static const struct option long_options[] = {
|
2017-06-14 13:25:33 +02:00
|
|
|
{"help", 0, NULL, 'h'},
|
|
|
|
{"verbose", 1, NULL, 'v'},
|
|
|
|
{"compile", 1, NULL, 'c'},
|
2019-12-12 19:00:42 +01:00
|
|
|
{"normalize", 1, NULL, 'n'},
|
2017-06-14 13:25:33 +02:00
|
|
|
{"output", 1, NULL, 'o'},
|
2019-12-12 19:18:28 +01:00
|
|
|
{"sort", 0, NULL, 's'},
|
2015-08-04 17:23:03 +02:00
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
2019-12-12 19:00:42 +01:00
|
|
|
char *source_file = NULL, *normalize_file = NULL, *output_file = NULL;
|
2019-12-12 19:18:28 +01:00
|
|
|
int c, err, verbose = 0, sort = 0, option_index;
|
2015-08-04 17:23:03 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
err = snd_output_stdio_attach(&log, stderr, 0);
|
|
|
|
assert(err >= 0);
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'v':
|
|
|
|
verbose = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
source_file = optarg;
|
|
|
|
break;
|
2019-12-12 19:00:42 +01:00
|
|
|
case 'n':
|
|
|
|
normalize_file = optarg;
|
|
|
|
break;
|
2015-08-04 17:23:03 +02:00
|
|
|
case 'o':
|
|
|
|
output_file = optarg;
|
|
|
|
break;
|
2019-12-12 19:18:28 +01:00
|
|
|
case 's':
|
|
|
|
sort = 1;
|
|
|
|
break;
|
2015-08-04 17:23:03 +02:00
|
|
|
default:
|
|
|
|
fprintf(stderr, _("Try `%s --help' for more information.\n"), argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:00:42 +01:00
|
|
|
if (source_file && normalize_file) {
|
|
|
|
fprintf(stderr, "Cannot normalize and compile at a time!\n");
|
2015-08-04 17:23:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:00:42 +01:00
|
|
|
if ((source_file == NULL && normalize_file == NULL) || output_file == NULL) {
|
|
|
|
usage(argv[0]);
|
2015-08-04 17:23:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:00:42 +01:00
|
|
|
if (source_file)
|
|
|
|
err = compile(source_file, output_file, verbose);
|
|
|
|
else
|
2019-12-12 19:18:28 +01:00
|
|
|
err = normalize(normalize_file, output_file, sort);
|
|
|
|
|
|
|
|
snd_output_close(log);
|
2015-08-04 17:23:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|