Mercurial > pidgin
annotate src/gtksound.c @ 11538:5c6c2e893803
[gaim-migrate @ 13792]
Don't crash if logging out while connecting
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Wed, 14 Sep 2005 01:18:07 +0000 |
parents | 448ebda0f7ec |
children | 11d30825c1bb |
rev | line source |
---|---|
5684 | 1 /* |
10297
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10158
diff
changeset
|
2 * @file gtksound.h GTK+ Sound |
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10158
diff
changeset
|
3 * @ingroup gtkui |
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10158
diff
changeset
|
4 * |
5684 | 5 * gaim |
6 * | |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
5684 | 10 * |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 * | |
25 */ | |
9791 | 26 #include "internal.h" |
27 #include "gtkgaim.h" | |
5684 | 28 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
29 #ifdef _WIN32 |
5684 | 30 #include <windows.h> |
31 #include <mmsystem.h> | |
32 #endif | |
33 | |
34 #ifdef USE_AO | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
35 # include <ao/ao.h> |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
36 # include <audiofile.h> |
5684 | 37 #endif /* USE_AO */ |
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" |
7465 | 43 #include "util.h" |
5684 | 44 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
45 #include "gtksound.h" |
5684 | 46 |
47 struct gaim_sound_event { | |
48 char *label; | |
49 char *pref; | |
50 char *def; | |
51 }; | |
52 | |
10074 | 53 #define PLAY_SOUND_TIMEOUT 15000 |
5684 | 54 |
10320 | 55 static guint mute_login_sounds_timeout = 0; |
5684 | 56 static gboolean mute_login_sounds = FALSE; |
6199 | 57 static gboolean sound_initialized = FALSE; |
5684 | 58 |
59 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { | |
10158 | 60 {N_("Buddy logs in"), "login", "login.wav"}, |
61 {N_("Buddy logs out"), "logout", "logout.wav"}, | |
5684 | 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"}, | |
10158 | 65 {N_("Person enters chat"), "join_chat", "login.wav"}, |
66 {N_("Person leaves chat"), "left_chat", "logout.wav"}, | |
5684 | 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 ;-) */ | |
10158 | 70 {NULL, "pounce_default", "alert.wav"}, |
71 {N_("Someone says your name in chat"), "nick_said", "alert.wav"} | |
5684 | 72 }; |
73 | |
74 #ifdef USE_AO | |
75 static int ao_driver = -1; | |
76 #endif /* USE_AO */ | |
77 | |
10320 | 78 static gboolean |
11463 | 79 unmute_login_sounds_cb(gpointer data) |
10320 | 80 { |
81 mute_login_sounds = FALSE; | |
82 mute_login_sounds_timeout = 0; | |
83 return FALSE; | |
84 } | |
85 | |
86 /* | |
87 * We mute sounds for the 10 seconds after you log in so that | |
88 * you don't get flooded with sounds when the blist shows all | |
89 * your buddies logging in. | |
90 */ | |
91 static void | |
92 account_signon_cb(GaimConnection *gc, gpointer data) | |
93 { | |
94 if (mute_login_sounds_timeout != 0) | |
95 g_source_remove(mute_login_sounds_timeout); | |
96 mute_login_sounds = TRUE; | |
11463 | 97 mute_login_sounds_timeout = gaim_timeout_add(10000, unmute_login_sounds_cb, NULL); |
10320 | 98 } |
99 | |
10322 | 100 static void |
101 _pref_sound_method_changed(const char *name, GaimPrefType type, | |
102 gpointer val, gpointer data) { | |
103 if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method")) | |
104 return; | |
105 | |
106 sound_initialized = TRUE; | |
107 | |
108 #ifdef USE_AO | |
109 ao_driver = -1; | |
110 | |
111 if(!strcmp(val, "esd")) | |
112 ao_driver = ao_driver_id("esd"); | |
113 else if(!strcmp(val, "arts")) | |
114 ao_driver = ao_driver_id("arts"); | |
11082 | 115 else if(!strcmp(val, "nas")) |
116 ao_driver = ao_driver_id("nas"); | |
10322 | 117 else if(!strcmp(val, "automatic")) |
118 ao_driver = ao_default_driver_id(); | |
119 | |
120 if(ao_driver != -1) { | |
121 ao_info *info = ao_driver_info(ao_driver); | |
122 gaim_debug_info("sound", | |
123 "Sound output driver loaded: %s\n", info->name); | |
124 } | |
125 #endif /* USE_AO */ | |
126 } | |
127 | |
128 const char * | |
129 gaim_gtk_sound_get_event_option(GaimSoundEventID event) | |
130 { | |
131 if(event >= GAIM_NUM_SOUNDS) | |
132 return 0; | |
133 | |
134 return sounds[event].pref; | |
135 } | |
136 | |
137 char * | |
138 gaim_gtk_sound_get_event_label(GaimSoundEventID event) | |
139 { | |
140 if(event >= GAIM_NUM_SOUNDS) | |
141 return NULL; | |
142 | |
143 return sounds[event].label; | |
144 } | |
145 | |
146 void * | |
147 gaim_gtk_sound_get_handle() | |
148 { | |
149 static int handle; | |
150 | |
151 return &handle; | |
152 } | |
153 | |
154 static void | |
155 gaim_gtk_sound_init(void) | |
5684 | 156 { |
10320 | 157 void *gtk_sound_handle = gaim_gtk_sound_get_handle(); |
158 | |
159 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", | |
160 gtk_sound_handle, GAIM_CALLBACK(account_signon_cb), | |
161 NULL); | |
162 | |
5684 | 163 gaim_prefs_add_none("/gaim/gtk/sound"); |
164 gaim_prefs_add_none("/gaim/gtk/sound/enabled"); | |
165 gaim_prefs_add_none("/gaim/gtk/sound/file"); | |
166 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE); | |
167 gaim_prefs_add_string("/gaim/gtk/sound/file/login", ""); | |
168 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE); | |
169 gaim_prefs_add_string("/gaim/gtk/sound/file/logout", ""); | |
170 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE); | |
171 gaim_prefs_add_string("/gaim/gtk/sound/file/im_recv", ""); | |
172 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE); | |
173 gaim_prefs_add_string("/gaim/gtk/sound/file/first_im_recv", ""); | |
174 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE); | |
175 gaim_prefs_add_string("/gaim/gtk/sound/file/send_im", ""); | |
176 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE); | |
177 gaim_prefs_add_string("/gaim/gtk/sound/file/join_chat", ""); | |
178 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE); | |
179 gaim_prefs_add_string("/gaim/gtk/sound/file/left_chat", ""); | |
180 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE); | |
181 gaim_prefs_add_string("/gaim/gtk/sound/file/send_chat_msg", ""); | |
182 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE); | |
183 gaim_prefs_add_string("/gaim/gtk/sound/file/chat_msg_recv", ""); | |
184 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE); | |
185 gaim_prefs_add_string("/gaim/gtk/sound/file/nick_said", ""); | |
7460
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
186 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/pounce_default", TRUE); |
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
187 gaim_prefs_add_string("/gaim/gtk/sound/file/pounce_default", ""); |
8633 | 188 gaim_prefs_add_bool("/gaim/gtk/sound/conv_focus", TRUE); |
10074 | 189 gaim_prefs_add_bool("/gaim/gtk/sound/mute", FALSE); |
5684 | 190 gaim_prefs_add_string("/gaim/gtk/sound/command", ""); |
191 gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic"); | |
192 | |
193 #ifdef USE_AO | |
10322 | 194 gaim_debug_info("sound", "Initializing sound output drivers.\n"); |
5684 | 195 ao_initialize(); |
196 #endif /* USE_AO */ | |
197 | |
10087 | 198 gaim_prefs_connect_callback(gaim_gtk_sound_get_handle(), "/gaim/gtk/sound/method", |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
199 _pref_sound_method_changed, NULL); |
5684 | 200 } |
201 | |
10322 | 202 static void |
203 gaim_gtk_sound_uninit(void) | |
5684 | 204 { |
205 #ifdef USE_AO | |
206 ao_shutdown(); | |
207 #endif | |
6199 | 208 sound_initialized = FALSE; |
5684 | 209 } |
210 | |
11463 | 211 #ifdef USE_AO |
10322 | 212 static gboolean |
213 expire_old_child(gpointer data) | |
10074 | 214 { |
215 int ret; | |
216 pid_t pid = GPOINTER_TO_INT(data); | |
217 | |
218 ret = waitpid(pid, NULL, WNOHANG | WUNTRACED); | |
219 | |
220 if(ret == 0) { | |
221 if(kill(pid, SIGKILL) < 0) | |
10322 | 222 gaim_debug_error("gtksound", "Killing process %d failed (%s)\n", |
223 pid, strerror(errno)); | |
10074 | 224 } |
225 | |
226 return FALSE; /* do not run again */ | |
227 } | |
228 #endif | |
229 | |
10322 | 230 static void |
231 gaim_gtk_sound_play_file(const char *filename) | |
5684 | 232 { |
233 const char *method; | |
11463 | 234 #ifdef USE_AO |
5684 | 235 pid_t pid; |
236 AFfilehandle file; | |
237 #endif | |
238 | |
6199 | 239 if (!sound_initialized) |
240 gaim_prefs_trigger_callback("/gaim/gtk/sound/method"); | |
241 | |
10074 | 242 if (gaim_prefs_get_bool("/gaim/gtk/sound/mute")) |
5684 | 243 return; |
244 | |
245 method = gaim_prefs_get_string("/gaim/gtk/sound/method"); | |
246 | |
10074 | 247 if (!strcmp(method, "none")) { |
248 return; | |
249 } else if (!strcmp(method, "beep")) { | |
5684 | 250 gdk_beep(); |
251 return; | |
252 } | |
253 | |
254 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
255 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename); | |
256 gaim_notify_error(NULL, NULL, tmp, NULL); | |
257 g_free(tmp); | |
258 return; | |
259 } | |
260 | |
261 #ifndef _WIN32 | |
262 if (!strcmp(method, "custom")) { | |
263 const char *sound_cmd; | |
264 char *command; | |
265 GError *error = NULL; | |
266 | |
267 sound_cmd = gaim_prefs_get_string("/gaim/gtk/sound/command"); | |
268 | |
269 if (!sound_cmd || *sound_cmd == '\0') { | |
270 gaim_notify_error(NULL, NULL, | |
271 _("Unable to play sound because the " | |
272 "'Command' sound method has been chosen, " | |
273 "but no command has been set."), NULL); | |
274 return; | |
275 } | |
276 | |
7464 | 277 if(strstr(sound_cmd, "%s")) |
278 command = gaim_strreplace(sound_cmd, "%s", filename); | |
279 else | |
280 command = g_strdup_printf("%s %s", sound_cmd, filename); | |
5684 | 281 |
282 if(!g_spawn_command_line_async(command, &error)) { | |
283 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
284 gaim_notify_error(NULL, NULL, tmp, NULL); | |
285 g_free(tmp); | |
286 g_error_free(error); | |
287 } | |
288 | |
289 g_free(command); | |
290 return; | |
291 } | |
11463 | 292 #ifdef USE_AO |
5684 | 293 pid = fork(); |
294 if (pid < 0) | |
295 return; | |
296 else if (pid == 0) { | |
11463 | 297 /* Child process */ |
5684 | 298 file = afOpenFile(filename, "rb", NULL); |
299 if(file) { | |
300 ao_device *device; | |
301 ao_sample_format format; | |
302 int in_fmt; | |
303 int bytes_per_frame; | |
304 | |
305 format.rate = afGetRate(file, AF_DEFAULT_TRACK); | |
306 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
307 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
308 &format.bits); | |
309 | |
310 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll | |
311 * let libaudiofile make them a bit better for us */ | |
312 if(format.bits == 8) | |
313 format.bits = 16; | |
314 | |
315 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, | |
316 AF_SAMPFMT_TWOSCOMP, format.bits); | |
317 | |
318 #if __BYTE_ORDER == __BIG_ENDIAN | |
319 format.byte_format = AO_FMT_BIG; | |
320 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
321 AF_BYTEORDER_BIGENDIAN); | |
322 #elif __BYTE_ORDER == __LITTLE_ENDIAN | |
323 format.byte_format = AO_FMT_LITTLE; | |
324 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
325 AF_BYTEORDER_LITTLEENDIAN); | |
326 #endif | |
327 | |
328 bytes_per_frame = format.bits * format.channels / 8; | |
329 | |
330 device = ao_open_live(ao_driver, &format, NULL); | |
331 | |
332 if(device) { | |
333 int frames_read; | |
334 char buf[4096]; | |
335 int buf_frames = sizeof(buf) / bytes_per_frame; | |
336 | |
337 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
338 buf, buf_frames))) { | |
339 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
340 break; | |
341 } | |
342 ao_close(device); | |
343 } | |
344 afCloseFile(file); | |
345 } | |
346 ao_shutdown(); | |
347 _exit(0); | |
10074 | 348 } else { |
11463 | 349 /* Parent process */ |
10074 | 350 gaim_timeout_add(PLAY_SOUND_TIMEOUT, expire_old_child, GINT_TO_POINTER(pid)); |
5684 | 351 } |
11082 | 352 #else /* USE_AO */ |
5684 | 353 gdk_beep(); |
354 return; | |
11082 | 355 #endif /* USE_AO */ |
5684 | 356 #else /* _WIN32 */ |
10322 | 357 gaim_debug_info("sound", "Playing %s\n", filename); |
5684 | 358 |
10922
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
359 if (G_WIN32_HAVE_WIDECHAR_API ()) { |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
360 wchar_t *wc_filename = g_utf8_to_utf16(filename, |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
361 -1, NULL, NULL, NULL); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
362 if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME)) |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
363 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
364 g_free(wc_filename); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
365 } else { |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
366 char *l_filename = g_locale_from_utf8(filename, |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
367 -1, NULL, NULL, NULL); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
368 if (!PlaySoundA(l_filename, NULL, SND_ASYNC | SND_FILENAME)) |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
369 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
370 g_free(l_filename); |
68083504217c
[gaim-migrate @ 12691]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10322
diff
changeset
|
371 } |
5684 | 372 #endif /* _WIN32 */ |
373 } | |
374 | |
10322 | 375 static void |
376 gaim_gtk_sound_play_event(GaimSoundEventID event) | |
5684 | 377 { |
378 char *enable_pref; | |
379 char *file_pref; | |
380 | |
381 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) | |
382 return; | |
383 | |
384 if (event >= GAIM_NUM_SOUNDS) { | |
10322 | 385 gaim_debug_error("sound", "got request for unknown sound: %d\n", event); |
5684 | 386 return; |
387 } | |
388 | |
389 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s", | |
390 sounds[event].pref); | |
391 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref); | |
392 | |
393 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
394 if (gaim_prefs_get_bool(enable_pref)) { | |
395 char *filename = g_strdup(gaim_prefs_get_string(file_pref)); | |
396 if(!filename || !strlen(filename)) { | |
397 if(filename) g_free(filename); | |
398 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); | |
399 } | |
400 | |
401 gaim_sound_play_file(filename); | |
402 g_free(filename); | |
403 } | |
404 | |
405 g_free(enable_pref); | |
406 g_free(file_pref); | |
407 } | |
408 | |
409 static GaimSoundUiOps sound_ui_ops = | |
410 { | |
411 gaim_gtk_sound_init, | |
10322 | 412 gaim_gtk_sound_uninit, |
5684 | 413 gaim_gtk_sound_play_file, |
414 gaim_gtk_sound_play_event | |
415 }; | |
416 | |
7035
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
417 GaimSoundUiOps * |
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
418 gaim_gtk_sound_get_ui_ops(void) |
5684 | 419 { |
420 return &sound_ui_ops; | |
421 } |