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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
#define DEFAULT_THEME "dudeldidei"
#define DEFAULT_PROFILE "stereo"
static int find_sound_for_suffix(ca_sound_file **f, const char *path, const char *name, const char *suffix, const char *theme, const char *locale, const char *subdir) {
const char *fn;
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(path, CA_ERROR_INVALID);
ca_return_val_if_fail(path[0] == '/', CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
ca_return_val_if_fail(suffix, CA_ERROR_INVALID);
if (!(fn = ca_sprintf_malloc("%s/%s%s%s%s%s%s",
path,
theme ? "/" : "",
theme ? theme : "",
subdir ? "/" : ""
subdir ? subdir : "",
locale ? "/" : "",
locale ? locale : "",
name, suffix)))
return CA_ERROR_OOM;
ret = ca_sound_file_open(f, fn);
ca_free(fn);
return ret;
}
static int find_sound_in_path(ca_sound_file **f, const char *path, const char *name, const char *theme, const char *locale, const char *subdir) {
int ret;
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(path, CA_ERROR_INVALID);
ca_return_val_if_fail(path[0] == '/', CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
if ((ret = find_sound_for_suffix(f, path, name, ".ogg", theme, locale, subdir)) != CA_ERROR_NOTFOUND)
return ret;
return find_sound_for_suffix(f, path, name, ".wav", theme, locale, subdir);
}
static int find_sound_in_theme(ca_sound_file **f, const char *name, const char *theme, const char *locale, const char *subdir) {
int ret;
const char *e;
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
if ((e = getenv("XDG_DATA_DIRS"))) {
for (;;) {
const char *r;
char *p;
if (!(r = strchr(e, ':')))
break;
if (!(p = ca_strndup(e, r-e)))
return CA_ERROR_OOM;
ret = find_sound_in_path(f, p, name, theme, locale, subdir);
ca_free(p);
if (ret != CA_ERROR_NOTFOUND)
return ret;
e = r+1;
}
}
if ((e = getenv("HOME"))) {
char *p;
#define SUBDIR "/.share/sounds"
if (!(p = ca_new(char, strlen(e) + sizeof(SUBDIR))))
return CA_ERROR_OOM;
sprintf(p, "%s" SUBDIR, e);
ret = find_sound_in_path(f, p, name, theme, locale, subdir);
ca_free(p);
if (ret != CA_ERROR_NOTFOUND)
return ret;
}
return CA_ERROR_NOTFOUND;
}
static int find_sound_in_locale(ca_sound_file **f, const char *name, const char *theme, const char *locale, const char *profile) {
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
ca_return_val_if_fail(profile, CA_ERROR_INVALID);
/* First, try the profile def itself */
if ((ret = find_sound_in_profile(f, name, theme, locale, profile)) != CA_ERROR_NOTFOUND)
return ret;
/* Then, fall back to stereo */
if (strcmp(profile, "stereo"))
if ((ret = find_sound_in_profile(f, name, theme, locale, "stereo")) != CA_ERROR_NOTFOUND)
return ret;
/* And fall back to no profile */
return find_sound_in_profile(f, name, theme, locale, NULL);
}
static int find_sound_in_theme(ca_sound_file **f, const char *name, const char *theme, const char *locale, const char *profile) {
const char *e;
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
ca_return_val_if_fail(locale, CA_ERROR_INVALID);
ca_return_val_if_fail(profile, CA_ERROR_INVALID);
/* First, try the locale def itself */
if ((ret = find_sound_in_locale(f, name, theme, locale, profile)) != CA_ERROR_NOTFOUND)
return ret;
/* Then, try to truncate at the @ */
if ((e = strchr(locale, '@'))) {
char *t;
if (!(t = ca_strndup(t, e - locale)))
return CA_ERROR_OOM;
ret = find_sound_in_locale(f, name, theme, t, profile);
ca_free(t);
if (ret != CA_ERROR_NOTFOUND)
return ret;
}
/* Followed by truncating at the _ */
if ((e = strchr(locale, '_'))) {
char *t;
if (!(t = ca_strndup(t, e - locale)))
return CA_ERROR_OOM;
ret = find_sound_in_locale(f, name, theme, t, profile);
ca_free(t);
if (ret != CA_ERROR_NOTFOUND)
return ret;
}
/* Then, try "C" as fallback locale */
if (strcmp(locale, "C"))
if ((ret = find_sound_in_locale(f, name, theme, "C", profile)) != CA_ERROR_NOTFOUND)
return ret;
/* Try without locale */
if ((ret = find_sound_in_locale(f, name, theme, NULL, profile)))
return ret;
/* Try to find an inherited theme */
}
int find_sound(ca_sound_file **f, const char *name, const char *theme, const char *locale, const char *profile) {
int ret;
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(name, CA_ERROR_INVALID);
if (!theme)
theme = DEFAULT_THEME;
if (!locale)
locale = setlocale(LC_MESSAGES, NULL);
if (!locale)
locale = "C";
if (!profile)
profile = DEFAULT_PROFILE;
/* First, try in the theme itself */
if ((ret = find_sound_in_theme(f, name, theme, locale, profile)) != CA_ERROR_NOTFOUND)
return ret;
/* Then, fall back to the magic freedesktop theme */
if (strcmp(theme, "freedesktop"))
if ((ret = find_sound_in_theme(f, name, "freedesktop", locale, profile)) != CA_ERROR_NOTFOUND)
return ret;
/* Finally, fall back to "unthemed" files */
return find_sound_in_theme(f, name, NULL, locale, profile);
}
FindSound(sound, locale) {
filename = FindSoundHelper(sound, locale, soundsystem, user selected theme);
if filename != none
return filename
filename = FindSoundHelper(sound, locale, soundsystem, "freedesktop");
if filename != none
return filename
return LookupFallbackSound (sound)
}
FindSoundHelper(sound, locale, soundsystem, theme) {
filename = LookupSound (sound, locale, soundsystem, theme)
if filename != none
return filename
if theme has parents
parents = theme.parents
for parent in parents {
filename = FindSoundHelper (sound, locale, soundsystem, parent)
if filename != none
return filename
}
return none
}
LookupSound (sound, locale, soundsystem, theme) {
// lookup localized version
for each subdir in $(theme subdir list) {
for each directory in $(basename list) {
for system in (soundsystem, "stereo") {
if DirectoryMatchesSoundSystem(subdir, system) {
for extension in ("wav", "ogg") {
filename = directory/$(themename)/subdir/$locale/sound.extension
if exist filename
return filename
}
}
}
}
}
// lookup unlocalized version
for each subdir in $(theme subdir list) {
for each directory in $(basename list) {
for system in (soundsystem, "stereo") {
if DirectoryMatchesSoundSystem(subdir, system) {
for extension in ("wav", "ogg") {
filename = directory/$(themename)/subdir/sound.extension
if exist filename
return filename
}
}
}
}
}
return none
}
LookupFallbackSound (sound) {
for each directory in $(basename list) {
for extension in ("wav", "ogg") {
if exists directory/sound.extension
return directory/sound.extension
}
}
return none
}
DirectoryMatchesSoundSystem(subdir, system) {
read SoundSystem from subdir
if SoundSystem == system
return true
return false
}
int ca_resolve_file(ca_sound_file *f, ca_proplist *p) {
ca_return_val_if_fail(f, CA_ERROR_INVALID);
ca_return_val_if_fail(p, CA_ERROR_INVALID);
}
|