Fix more warnings. Use -Wall flag to enable those warnings to show up in first place.
[calfbox.git] / hwcfg.c
blob8513feab753b0a7c50ffe31f96d4905fc88d2e29
1 /*
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/>.
19 #include "config.h"
20 #include "config-api.h"
21 #include "io.h"
23 #include <glib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
28 static char *cfg(const char *section, const char *name, char *defvalue)
30 char *value = cbox_config_get_string(section, name);
31 if (value)
32 return value;
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");
39 FILE *f;
40 if (!rcfile)
42 rcfile = g_strdup_printf("%s/.jackdrc", getenv("HOME"));
43 g_message("Generating JACK config: %s\n", rcfile);
44 f = fopen(rcfile, "w");
45 if (!f)
47 g_error("Cannot open file %s", rcfile);
48 return;
50 g_free(rcfile);
52 else
54 g_message("Generating JACK config: %s\n", rcfile);
55 f = fopen(rcfile, "w");
56 if (!f)
58 g_error("Cannot open file %s", rcfile);
59 return;
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"),
66 id,
67 cfg(section, "alsa_options", ""));
68 fclose(f);
71 static int try_soundcard(const char *name)
73 gchar *id;
74 if (!cbox_config_has_section(name))
75 return 0;
77 g_message("Trying section %s", name);
79 id = cbox_config_get_string(name, "device");
80 if (id != NULL)
82 struct stat s;
83 int result;
84 gchar *fn = g_strdup_printf("/proc/asound/%s", id);
85 result = stat(fn, &s);
86 if (!result)
87 generate_jack_config(name, id);
88 g_free(fn);
89 return !result;
92 id = cbox_config_get_string(name, "usbid");
93 if (id != NULL)
95 int vid, pid;
96 if (sscanf(id, "%x:%x\n", &vid, &pid) !=2)
98 g_error("Invalid VID:PID value: %s", id);
99 return 0;
101 for (int i = 0; ; i++)
103 struct stat s;
104 int result;
105 FILE *f = NULL;
106 int tvid, tpid;
108 // check if it's not beyond the last soundcard index
109 gchar *fn = g_strdup_printf("/proc/asound/card%d", i);
110 result = stat(fn, &s);
111 g_free(fn);
112 if (result)
113 break;
115 // check if it has a USB ID
116 fn = g_strdup_printf("/proc/asound/card%d/usbid", i);
117 f = fopen(fn, "r");
118 g_free(fn);
120 if (!f)
121 continue;
123 if (fscanf(f, "%x:%x", &tvid, &tpid) == 2)
125 if (vid == tvid && pid == tpid)
127 gchar *fn = g_strdup_printf("%d", i);
128 generate_jack_config(name, fn);
129 g_free(fn);
130 fclose(f);
131 return 1;
134 fclose(f);
136 return 0;
139 return 0;
142 int cbox_hwcfg_setup_jack(void)
144 int i;
145 if (!cbox_config_has_section("autojack"))
146 return 0;
148 for (i = 0; ; i++)
150 int result;
151 gchar *cardnum = g_strdup_printf("soundcard%d", i);
152 char *secname = cbox_config_get_string("autojack", cardnum);
153 g_free(cardnum);
155 if (!secname)
156 break;
158 secname = g_strdup_printf("soundcard:%s", secname);
159 result = try_soundcard(secname);
160 g_free(secname);
162 if (result)
163 return 1;
166 return 0;