Mercurial > pidgin
annotate src/gtksound.c @ 7464:a259e244e44f
[gaim-migrate @ 8077]
fix "the % instead of %s in sound command causes segfault" bug
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 08 Nov 2003 21:32:13 +0000 |
parents | 3973a09525b3 |
children | dedbfdc7ce65 |
rev | line source |
---|---|
5684 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
6371
8f94cce8faa5
[gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents:
6199
diff
changeset
|
22 #include "gtkinternal.h" |
5684 | 23 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
24 #ifdef _WIN32 |
5684 | 25 #include <windows.h> |
26 #include <mmsystem.h> | |
27 #endif | |
28 | |
29 #ifdef USE_AO | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
30 # include <ao/ao.h> |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
31 # include <audiofile.h> |
5684 | 32 #endif /* USE_AO */ |
33 | |
34 #ifdef USE_NAS_AUDIO | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
35 # include <audio/audiolib.h> |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
36 # include <audio/soundlib.h> |
5684 | 37 #endif /* USE_NAS_AUDIO */ |
38 | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
39 #include "debug.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
40 #include "notify.h" |
5684 | 41 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
42 #include "sound.h" |
5684 | 43 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
44 #include "gtksound.h" |
5684 | 45 |
46 | |
47 struct gaim_sound_event { | |
48 char *label; | |
49 char *pref; | |
50 char *def; | |
51 }; | |
52 | |
53 | |
54 | |
55 static gboolean mute_login_sounds = FALSE; | |
56 static gboolean mute_sounds = FALSE; | |
6199 | 57 static gboolean sound_initialized = FALSE; |
5684 | 58 |
59 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { | |
60 {N_("Buddy logs in"), "login", "arrive.wav"}, | |
61 {N_("Buddy logs out"), "logout", "leave.wav"}, | |
62 {N_("Message received"), "im_recv", "receive.wav"}, | |
63 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"}, | |
64 {N_("Message sent"), "send_im", "send.wav"}, | |
65 {N_("Person enters chat"), "join_chat", "arrive.wav"}, | |
66 {N_("Person leaves chat"), "left_chat", "leave.wav"}, | |
67 {N_("You talk in chat"), "send_chat_msg", "send.wav"}, | |
68 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"}, | |
69 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ | |
70 {NULL, "pounce_default", "redalert.wav"}, | |
71 {N_("Someone says your name in chat"), "nick_said", "redalert.wav"} | |
72 }; | |
73 | |
74 #ifdef USE_AO | |
75 static int ao_driver = -1; | |
76 #endif /* USE_AO */ | |
77 | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
78 static void _pref_sound_method_changed(const char *name, GaimPrefType type, |
5684 | 79 gpointer val, gpointer data); |
80 | |
81 static void gaim_gtk_sound_init(void) | |
82 { | |
83 gaim_prefs_add_none("/gaim/gtk/sound"); | |
84 gaim_prefs_add_none("/gaim/gtk/sound/enabled"); | |
85 gaim_prefs_add_none("/gaim/gtk/sound/file"); | |
86 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE); | |
87 gaim_prefs_add_string("/gaim/gtk/sound/file/login", ""); | |
88 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE); | |
89 gaim_prefs_add_string("/gaim/gtk/sound/file/logout", ""); | |
90 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE); | |
91 gaim_prefs_add_string("/gaim/gtk/sound/file/im_recv", ""); | |
92 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE); | |
93 gaim_prefs_add_string("/gaim/gtk/sound/file/first_im_recv", ""); | |
94 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE); | |
95 gaim_prefs_add_string("/gaim/gtk/sound/file/send_im", ""); | |
96 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE); | |
97 gaim_prefs_add_string("/gaim/gtk/sound/file/join_chat", ""); | |
98 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE); | |
99 gaim_prefs_add_string("/gaim/gtk/sound/file/left_chat", ""); | |
100 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE); | |
101 gaim_prefs_add_string("/gaim/gtk/sound/file/send_chat_msg", ""); | |
102 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE); | |
103 gaim_prefs_add_string("/gaim/gtk/sound/file/chat_msg_recv", ""); | |
104 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE); | |
105 gaim_prefs_add_string("/gaim/gtk/sound/file/nick_said", ""); | |
7460
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
106 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/pounce_default", TRUE); |
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
107 gaim_prefs_add_string("/gaim/gtk/sound/file/pounce_default", ""); |
5684 | 108 gaim_prefs_add_bool("/gaim/gtk/sound/silent_signon", TRUE); |
109 gaim_prefs_add_string("/gaim/gtk/sound/command", ""); | |
110 gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic"); | |
111 | |
112 #ifdef USE_AO | |
113 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
114 "Initializing sound output drivers.\n"); | |
115 ao_initialize(); | |
116 #endif /* USE_AO */ | |
117 | |
118 gaim_prefs_connect_callback("/gaim/gtk/sound/method", | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
119 _pref_sound_method_changed, NULL); |
5684 | 120 } |
121 | |
122 | |
123 static void gaim_gtk_sound_shutdown(void) | |
124 { | |
125 #ifdef USE_AO | |
126 ao_shutdown(); | |
127 #endif | |
6199 | 128 sound_initialized = FALSE; |
5684 | 129 } |
130 | |
131 static void gaim_gtk_sound_play_file(const char *filename) | |
132 { | |
133 const char *method; | |
134 #if defined(USE_NAS_AUDIO) || defined(USE_AO) | |
135 pid_t pid; | |
136 #ifdef USE_AO | |
137 AFfilehandle file; | |
138 #endif | |
139 #endif | |
140 | |
6199 | 141 if (!sound_initialized) |
142 gaim_prefs_trigger_callback("/gaim/gtk/sound/method"); | |
143 | |
5684 | 144 if (mute_sounds) |
145 return; | |
146 | |
147 method = gaim_prefs_get_string("/gaim/gtk/sound/method"); | |
148 | |
149 if (!strcmp(method, "beep")) { | |
150 gdk_beep(); | |
151 return; | |
152 } | |
153 | |
154 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
155 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename); | |
156 gaim_notify_error(NULL, NULL, tmp, NULL); | |
157 g_free(tmp); | |
158 return; | |
159 } | |
160 | |
161 #ifndef _WIN32 | |
162 if (!strcmp(method, "custom")) { | |
163 const char *sound_cmd; | |
164 char *command; | |
165 GError *error = NULL; | |
166 | |
167 sound_cmd = gaim_prefs_get_string("/gaim/gtk/sound/command"); | |
168 | |
169 if (!sound_cmd || *sound_cmd == '\0') { | |
170 gaim_notify_error(NULL, NULL, | |
171 _("Unable to play sound because the " | |
172 "'Command' sound method has been chosen, " | |
173 "but no command has been set."), NULL); | |
174 return; | |
175 } | |
176 | |
7464 | 177 if(strstr(sound_cmd, "%s")) |
178 command = gaim_strreplace(sound_cmd, "%s", filename); | |
179 else | |
180 command = g_strdup_printf("%s %s", sound_cmd, filename); | |
5684 | 181 |
182 if(!g_spawn_command_line_async(command, &error)) { | |
183 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
184 gaim_notify_error(NULL, NULL, tmp, NULL); | |
185 g_free(tmp); | |
186 g_error_free(error); | |
187 } | |
188 | |
189 g_free(command); | |
190 return; | |
191 } | |
192 #if defined(USE_NAS_AUDIO) || defined(USE_AO) | |
193 pid = fork(); | |
194 if (pid < 0) | |
195 return; | |
196 else if (pid == 0) { | |
197 #ifdef USE_NAS_AUDIO | |
198 if (!strcmp(method, "nas")) { | |
199 if (play_file_nas(filename)) | |
200 _exit(0); | |
201 } | |
202 #endif /* USE_NAS_AUDIO */ | |
203 | |
204 #ifdef USE_AO | |
205 file = afOpenFile(filename, "rb", NULL); | |
206 if(file) { | |
207 ao_device *device; | |
208 ao_sample_format format; | |
209 int in_fmt; | |
210 int bytes_per_frame; | |
211 | |
212 format.rate = afGetRate(file, AF_DEFAULT_TRACK); | |
213 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
214 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
215 &format.bits); | |
216 | |
217 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll | |
218 * let libaudiofile make them a bit better for us */ | |
219 if(format.bits == 8) | |
220 format.bits = 16; | |
221 | |
222 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, | |
223 AF_SAMPFMT_TWOSCOMP, format.bits); | |
224 | |
225 #if __BYTE_ORDER == __BIG_ENDIAN | |
226 format.byte_format = AO_FMT_BIG; | |
227 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
228 AF_BYTEORDER_BIGENDIAN); | |
229 #elif __BYTE_ORDER == __LITTLE_ENDIAN | |
230 format.byte_format = AO_FMT_LITTLE; | |
231 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
232 AF_BYTEORDER_LITTLEENDIAN); | |
233 #endif | |
234 | |
235 bytes_per_frame = format.bits * format.channels / 8; | |
236 | |
237 device = ao_open_live(ao_driver, &format, NULL); | |
238 | |
239 if(device) { | |
240 int frames_read; | |
241 char buf[4096]; | |
242 int buf_frames = sizeof(buf) / bytes_per_frame; | |
243 | |
244 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
245 buf, buf_frames))) { | |
246 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
247 break; | |
248 } | |
249 ao_close(device); | |
250 } | |
251 afCloseFile(file); | |
252 } | |
253 ao_shutdown(); | |
254 #endif /* USE_AO */ | |
255 _exit(0); | |
256 } | |
257 #else /* USE_NAS_AUDIO || USE_AO */ | |
258 gdk_beep(); | |
259 return; | |
260 #endif /* USE_NAS_AUDIO || USE_AO */ | |
261 #else /* _WIN32 */ | |
262 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename); | |
263 | |
264 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) | |
265 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); | |
266 #endif /* _WIN32 */ | |
267 } | |
268 | |
269 static void gaim_gtk_sound_play_event(GaimSoundEventID event) | |
270 { | |
271 char *enable_pref; | |
272 char *file_pref; | |
273 | |
274 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) | |
275 return; | |
276 | |
277 if (event >= GAIM_NUM_SOUNDS) { | |
278 gaim_debug(GAIM_DEBUG_MISC, "sound", | |
279 "got request for unknown sound: %d\n", event); | |
280 return; | |
281 } | |
282 | |
283 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s", | |
284 sounds[event].pref); | |
285 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref); | |
286 | |
287 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
288 if (gaim_prefs_get_bool(enable_pref)) { | |
289 char *filename = g_strdup(gaim_prefs_get_string(file_pref)); | |
290 if(!filename || !strlen(filename)) { | |
291 if(filename) g_free(filename); | |
292 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); | |
293 } | |
294 | |
295 gaim_sound_play_file(filename); | |
296 g_free(filename); | |
297 } | |
298 | |
299 g_free(enable_pref); | |
300 g_free(file_pref); | |
301 } | |
302 | |
303 static GaimSoundUiOps sound_ui_ops = | |
304 { | |
305 gaim_gtk_sound_init, | |
306 gaim_gtk_sound_shutdown, | |
307 gaim_gtk_sound_play_file, | |
308 gaim_gtk_sound_play_event | |
309 }; | |
310 | |
7035
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
311 GaimSoundUiOps * |
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
312 gaim_gtk_sound_get_ui_ops(void) |
5684 | 313 { |
314 return &sound_ui_ops; | |
315 } | |
316 | |
317 | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
318 static void _pref_sound_method_changed(const char *name, GaimPrefType type, |
5684 | 319 gpointer val, gpointer data) { |
6778 | 320 if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method")) |
321 return; | |
322 | |
6199 | 323 sound_initialized = TRUE; |
324 | |
5684 | 325 #ifdef USE_AO |
326 ao_driver = -1; | |
327 | |
328 if(!strcmp(val, "esd")) | |
329 ao_driver = ao_driver_id("esd"); | |
330 else if(!strcmp(val, "arts")) | |
331 ao_driver = ao_driver_id("arts"); | |
332 else if(!strcmp(val, "automatic")) | |
333 ao_driver = ao_default_driver_id(); | |
334 | |
335 if(ao_driver != -1) { | |
336 ao_info *info = ao_driver_info(ao_driver); | |
337 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
338 "Sound output driver loaded: %s\n", info->name); | |
339 } | |
340 #endif /* USE_AO */ | |
341 #ifdef USE_NAS | |
342 if (!strcmp(val, "nas")) | |
343 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
344 "Sound output driver loaded: NAS output\n"); | |
345 #endif /* USE_NAS */ | |
346 } | |
347 | |
348 #ifdef USE_NAS_AUDIO | |
349 static gboolean play_file_nas(const char *filename) | |
350 { | |
351 AuServer *nas_serv; | |
352 gboolean ret = FALSE; | |
353 | |
354 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) { | |
355 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100); | |
356 AuCloseServer(nas_serv); | |
357 } | |
358 | |
359 return ret; | |
360 } | |
361 | |
362 #endif /* USE_NAS_AUDIO */ | |
363 | |
364 void gaim_gtk_sound_set_mute(gboolean mute) | |
365 { | |
366 mute_sounds = mute; | |
367 } | |
368 | |
369 gboolean gaim_gtk_sound_get_mute() | |
370 { | |
371 return mute_sounds; | |
372 } | |
373 | |
374 void gaim_gtk_sound_set_login_mute(gboolean mute) | |
375 { | |
376 mute_login_sounds = mute; | |
377 } | |
378 | |
379 const char *gaim_gtk_sound_get_event_option(GaimSoundEventID event) | |
380 { | |
381 if(event >= GAIM_NUM_SOUNDS) | |
382 return 0; | |
383 | |
384 return sounds[event].pref; | |
385 } | |
386 | |
387 char *gaim_gtk_sound_get_event_label(GaimSoundEventID event) | |
388 { | |
389 if(event >= GAIM_NUM_SOUNDS) | |
390 return NULL; | |
391 | |
392 return sounds[event].label; | |
393 } | |
394 |