2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010 Krzysztof Foltman
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "config-api.h"
28 static char *cfg(const char *section
, const char *name
, char *defvalue
)
30 char *value
= cbox_config_get_string(section
, name
);
33 return cbox_config_get_string_with_default("autojack", name
, defvalue
);
36 static void generate_jack_config(const char *section
, const char *id
)
38 char *rcfile
= cbox_config_get_string("autojack", "jackdrc");
42 rcfile
= g_strdup_printf("%s/.jackdrc", getenv("HOME"));
43 g_message("Generating JACK config: %s\n", rcfile
);
44 f
= fopen(rcfile
, "w");
47 g_error("Cannot open file %s", rcfile
);
54 g_message("Generating JACK config: %s\n", rcfile
);
55 f
= fopen(rcfile
, "w");
58 g_error("Cannot open file %s", rcfile
);
63 fprintf(f
, "%s %s -d alsa -d hw:%s -r 44100 %s\n",
64 cfg(section
, "jackd", "/usr/bin/jackd"),
65 cfg(section
, "jack_options", "-R -T"),
67 cfg(section
, "alsa_options", ""));
71 static int try_soundcard(const char *name
)
74 if (!cbox_config_has_section(name
))
77 g_message("Trying section %s", name
);
79 id
= cbox_config_get_string(name
, "device");
84 gchar
*fn
= g_strdup_printf("/proc/asound/%s", id
);
85 result
= stat(fn
, &s
);
87 generate_jack_config(name
, id
);
92 id
= cbox_config_get_string(name
, "usbid");
96 if (sscanf(id
, "%x:%x\n", &vid
, &pid
) !=2)
98 g_error("Invalid VID:PID value: %s", id
);
102 for (int i
= 0; ; i
++)
109 // check if it's not beyond the last soundcard index
110 gchar
*fn
= g_strdup_printf("/proc/asound/card%d", i
);
111 result
= stat(fn
, &s
);
116 // check if it has a USB ID
117 fn
= g_strdup_printf("/proc/asound/card%d/usbid", i
);
124 if (fscanf(f
, "%x:%x", &tvid
, &tpid
) == 2)
126 if (vid
== tvid
&& pid
== tpid
)
128 gchar
*fn
= g_strdup_printf("%d", i
);
129 generate_jack_config(name
, fn
);
143 int cbox_hwcfg_setup_jack()
146 if (!cbox_config_has_section("autojack"))
152 gchar
*cardnum
= g_strdup_printf("soundcard%d", i
);
153 char *secname
= cbox_config_get_string("autojack", cardnum
);
159 secname
= g_strdup_printf("soundcard:%s", secname
);
160 result
= try_soundcard(secname
);