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/>.
23 #include "config-api.h"
31 static char *cfg(const char *section
, const char *name
, char *defvalue
)
33 char *value
= cbox_config_get_string(section
, name
);
36 return cbox_config_get_string_with_default("autojack", name
, defvalue
);
39 static void generate_jack_config(const char *section
, const char *id
)
41 char *rcfile
= cbox_config_get_string("autojack", "jackdrc");
45 rcfile
= g_strdup_printf("%s/.jackdrc", getenv("HOME"));
46 g_message("Generating JACK config: %s\n", rcfile
);
47 f
= fopen(rcfile
, "w");
50 g_error("Cannot open file %s", rcfile
);
57 g_message("Generating JACK config: %s\n", rcfile
);
58 f
= fopen(rcfile
, "w");
61 g_error("Cannot open file %s", rcfile
);
66 fprintf(f
, "%s %s -d alsa -d hw:%s -r 44100 %s\n",
67 cfg(section
, "jackd", "/usr/bin/jackd"),
68 cfg(section
, "jack_options", "-R -T"),
70 cfg(section
, "alsa_options", ""));
74 static int try_soundcard(const char *name
)
77 if (!cbox_config_has_section(name
))
80 g_message("Trying section %s", name
);
82 id
= cbox_config_get_string(name
, "device");
87 gchar
*fn
= g_strdup_printf("/proc/asound/%s", id
);
88 result
= stat(fn
, &s
);
90 generate_jack_config(name
, id
);
95 id
= cbox_config_get_string(name
, "usbid");
99 if (sscanf(id
, "%x:%x\n", &vid
, &pid
) !=2)
101 g_error("Invalid VID:PID value: %s", id
);
104 for (int i
= 0; ; i
++)
111 // check if it's not beyond the last soundcard index
112 gchar
*fn
= g_strdup_printf("/proc/asound/card%d", i
);
113 result
= stat(fn
, &s
);
118 // check if it has a USB ID
119 fn
= g_strdup_printf("/proc/asound/card%d/usbid", i
);
126 if (fscanf(f
, "%x:%x", &tvid
, &tpid
) == 2)
128 if (vid
== tvid
&& pid
== tpid
)
130 gchar
*fn
= g_strdup_printf("%d", i
);
131 generate_jack_config(name
, fn
);
145 int cbox_hwcfg_setup_jack(void)
148 if (!cbox_config_has_section("autojack"))
154 gchar
*cardnum
= g_strdup_printf("soundcard%d", i
);
155 char *secname
= cbox_config_get_string("autojack", cardnum
);
161 secname
= g_strdup_printf("soundcard:%s", secname
);
162 result
= try_soundcard(secname
);