Mercurial > pidgin.yaz
annotate src/gtksound.c @ 10320:61852117568f
[gaim-migrate @ 11527]
A little code cleanup here and there. And I removed gaim_setup() from
gtkmain.c and put it in gtksound.c
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 05 Dec 2004 21:05:52 +0000 |
parents | ec140184437b |
children | 2a132b73a6e6 |
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 | |
39 #ifdef USE_NAS_AUDIO | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
40 # include <audio/audiolib.h> |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
41 # include <audio/soundlib.h> |
5684 | 42 #endif /* USE_NAS_AUDIO */ |
43 | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
44 #include "debug.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
45 #include "notify.h" |
5684 | 46 #include "prefs.h" |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
47 #include "sound.h" |
7465 | 48 #include "util.h" |
5684 | 49 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5794
diff
changeset
|
50 #include "gtksound.h" |
5684 | 51 |
52 | |
53 struct gaim_sound_event { | |
54 char *label; | |
55 char *pref; | |
56 char *def; | |
57 }; | |
58 | |
10074 | 59 #define PLAY_SOUND_TIMEOUT 15000 |
5684 | 60 |
10320 | 61 static guint mute_login_sounds_timeout = 0; |
5684 | 62 static gboolean mute_login_sounds = FALSE; |
6199 | 63 static gboolean sound_initialized = FALSE; |
5684 | 64 |
65 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { | |
10158 | 66 {N_("Buddy logs in"), "login", "login.wav"}, |
67 {N_("Buddy logs out"), "logout", "logout.wav"}, | |
5684 | 68 {N_("Message received"), "im_recv", "receive.wav"}, |
69 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"}, | |
70 {N_("Message sent"), "send_im", "send.wav"}, | |
10158 | 71 {N_("Person enters chat"), "join_chat", "login.wav"}, |
72 {N_("Person leaves chat"), "left_chat", "logout.wav"}, | |
5684 | 73 {N_("You talk in chat"), "send_chat_msg", "send.wav"}, |
74 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"}, | |
75 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ | |
10158 | 76 {NULL, "pounce_default", "alert.wav"}, |
77 {N_("Someone says your name in chat"), "nick_said", "alert.wav"} | |
5684 | 78 }; |
79 | |
80 #ifdef USE_AO | |
81 static int ao_driver = -1; | |
82 #endif /* USE_AO */ | |
83 | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
84 static void _pref_sound_method_changed(const char *name, GaimPrefType type, |
5684 | 85 gpointer val, gpointer data); |
86 | |
10320 | 87 static gboolean |
88 mute_login_sounds_cb(gpointer data) | |
89 { | |
90 mute_login_sounds = FALSE; | |
91 mute_login_sounds_timeout = 0; | |
92 return FALSE; | |
93 } | |
94 | |
95 /* | |
96 * We mute sounds for the 10 seconds after you log in so that | |
97 * you don't get flooded with sounds when the blist shows all | |
98 * your buddies logging in. | |
99 */ | |
100 static void | |
101 account_signon_cb(GaimConnection *gc, gpointer data) | |
102 { | |
103 if (mute_login_sounds_timeout != 0) | |
104 g_source_remove(mute_login_sounds_timeout); | |
105 mute_login_sounds = TRUE; | |
106 mute_login_sounds_timeout = gaim_timeout_add(10000, mute_login_sounds_cb, NULL); | |
107 } | |
108 | |
5684 | 109 static void gaim_gtk_sound_init(void) |
110 { | |
10320 | 111 void *gtk_sound_handle = gaim_gtk_sound_get_handle(); |
112 | |
113 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", | |
114 gtk_sound_handle, GAIM_CALLBACK(account_signon_cb), | |
115 NULL); | |
116 | |
5684 | 117 gaim_prefs_add_none("/gaim/gtk/sound"); |
118 gaim_prefs_add_none("/gaim/gtk/sound/enabled"); | |
119 gaim_prefs_add_none("/gaim/gtk/sound/file"); | |
120 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE); | |
121 gaim_prefs_add_string("/gaim/gtk/sound/file/login", ""); | |
122 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE); | |
123 gaim_prefs_add_string("/gaim/gtk/sound/file/logout", ""); | |
124 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE); | |
125 gaim_prefs_add_string("/gaim/gtk/sound/file/im_recv", ""); | |
126 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE); | |
127 gaim_prefs_add_string("/gaim/gtk/sound/file/first_im_recv", ""); | |
128 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE); | |
129 gaim_prefs_add_string("/gaim/gtk/sound/file/send_im", ""); | |
130 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE); | |
131 gaim_prefs_add_string("/gaim/gtk/sound/file/join_chat", ""); | |
132 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE); | |
133 gaim_prefs_add_string("/gaim/gtk/sound/file/left_chat", ""); | |
134 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE); | |
135 gaim_prefs_add_string("/gaim/gtk/sound/file/send_chat_msg", ""); | |
136 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE); | |
137 gaim_prefs_add_string("/gaim/gtk/sound/file/chat_msg_recv", ""); | |
138 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE); | |
139 gaim_prefs_add_string("/gaim/gtk/sound/file/nick_said", ""); | |
7460
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
140 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/pounce_default", TRUE); |
3973a09525b3
[gaim-migrate @ 8073]
Christian Hammond <chipx86@chipx86.com>
parents:
7035
diff
changeset
|
141 gaim_prefs_add_string("/gaim/gtk/sound/file/pounce_default", ""); |
8633 | 142 gaim_prefs_add_bool("/gaim/gtk/sound/conv_focus", TRUE); |
10074 | 143 gaim_prefs_add_bool("/gaim/gtk/sound/mute", FALSE); |
5684 | 144 gaim_prefs_add_string("/gaim/gtk/sound/command", ""); |
145 gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic"); | |
146 | |
147 #ifdef USE_AO | |
148 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
149 "Initializing sound output drivers.\n"); | |
150 ao_initialize(); | |
151 #endif /* USE_AO */ | |
152 | |
10087 | 153 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
|
154 _pref_sound_method_changed, NULL); |
5684 | 155 } |
156 | |
157 | |
158 static void gaim_gtk_sound_shutdown(void) | |
159 { | |
160 #ifdef USE_AO | |
161 ao_shutdown(); | |
162 #endif | |
6199 | 163 sound_initialized = FALSE; |
5684 | 164 } |
165 | |
10074 | 166 #if defined(USE_NAS_AUDIO) || defined(USE_AO) |
167 gboolean expire_old_child(gpointer data) | |
168 { | |
169 int ret; | |
170 pid_t pid = GPOINTER_TO_INT(data); | |
171 | |
172 ret = waitpid(pid, NULL, WNOHANG | WUNTRACED); | |
173 | |
174 if(ret == 0) { | |
175 if(kill(pid, SIGKILL) < 0) | |
176 gaim_debug_error("gtksound", "Killing process %d failed (%s)\n", pid, strerror(errno)); | |
177 } | |
178 | |
179 return FALSE; /* do not run again */ | |
180 } | |
181 #endif | |
182 | |
5684 | 183 static void gaim_gtk_sound_play_file(const char *filename) |
184 { | |
185 const char *method; | |
186 #if defined(USE_NAS_AUDIO) || defined(USE_AO) | |
187 pid_t pid; | |
188 #ifdef USE_AO | |
189 AFfilehandle file; | |
190 #endif | |
191 #endif | |
192 | |
6199 | 193 if (!sound_initialized) |
194 gaim_prefs_trigger_callback("/gaim/gtk/sound/method"); | |
195 | |
10074 | 196 if (gaim_prefs_get_bool("/gaim/gtk/sound/mute")) |
5684 | 197 return; |
198 | |
199 method = gaim_prefs_get_string("/gaim/gtk/sound/method"); | |
200 | |
10074 | 201 if (!strcmp(method, "none")) { |
202 return; | |
203 } else if (!strcmp(method, "beep")) { | |
5684 | 204 gdk_beep(); |
205 return; | |
206 } | |
207 | |
208 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
209 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename); | |
210 gaim_notify_error(NULL, NULL, tmp, NULL); | |
211 g_free(tmp); | |
212 return; | |
213 } | |
214 | |
215 #ifndef _WIN32 | |
216 if (!strcmp(method, "custom")) { | |
217 const char *sound_cmd; | |
218 char *command; | |
219 GError *error = NULL; | |
220 | |
221 sound_cmd = gaim_prefs_get_string("/gaim/gtk/sound/command"); | |
222 | |
223 if (!sound_cmd || *sound_cmd == '\0') { | |
224 gaim_notify_error(NULL, NULL, | |
225 _("Unable to play sound because the " | |
226 "'Command' sound method has been chosen, " | |
227 "but no command has been set."), NULL); | |
228 return; | |
229 } | |
230 | |
7464 | 231 if(strstr(sound_cmd, "%s")) |
232 command = gaim_strreplace(sound_cmd, "%s", filename); | |
233 else | |
234 command = g_strdup_printf("%s %s", sound_cmd, filename); | |
5684 | 235 |
236 if(!g_spawn_command_line_async(command, &error)) { | |
237 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
238 gaim_notify_error(NULL, NULL, tmp, NULL); | |
239 g_free(tmp); | |
240 g_error_free(error); | |
241 } | |
242 | |
243 g_free(command); | |
244 return; | |
245 } | |
246 #if defined(USE_NAS_AUDIO) || defined(USE_AO) | |
247 pid = fork(); | |
248 if (pid < 0) | |
249 return; | |
250 else if (pid == 0) { | |
251 #ifdef USE_NAS_AUDIO | |
252 if (!strcmp(method, "nas")) { | |
253 if (play_file_nas(filename)) | |
254 _exit(0); | |
255 } | |
256 #endif /* USE_NAS_AUDIO */ | |
257 | |
258 #ifdef USE_AO | |
259 file = afOpenFile(filename, "rb", NULL); | |
260 if(file) { | |
261 ao_device *device; | |
262 ao_sample_format format; | |
263 int in_fmt; | |
264 int bytes_per_frame; | |
265 | |
266 format.rate = afGetRate(file, AF_DEFAULT_TRACK); | |
267 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
268 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
269 &format.bits); | |
270 | |
271 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll | |
272 * let libaudiofile make them a bit better for us */ | |
273 if(format.bits == 8) | |
274 format.bits = 16; | |
275 | |
276 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, | |
277 AF_SAMPFMT_TWOSCOMP, format.bits); | |
278 | |
279 #if __BYTE_ORDER == __BIG_ENDIAN | |
280 format.byte_format = AO_FMT_BIG; | |
281 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
282 AF_BYTEORDER_BIGENDIAN); | |
283 #elif __BYTE_ORDER == __LITTLE_ENDIAN | |
284 format.byte_format = AO_FMT_LITTLE; | |
285 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
286 AF_BYTEORDER_LITTLEENDIAN); | |
287 #endif | |
288 | |
289 bytes_per_frame = format.bits * format.channels / 8; | |
290 | |
291 device = ao_open_live(ao_driver, &format, NULL); | |
292 | |
293 if(device) { | |
294 int frames_read; | |
295 char buf[4096]; | |
296 int buf_frames = sizeof(buf) / bytes_per_frame; | |
297 | |
298 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
299 buf, buf_frames))) { | |
300 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
301 break; | |
302 } | |
303 ao_close(device); | |
304 } | |
305 afCloseFile(file); | |
306 } | |
307 ao_shutdown(); | |
308 #endif /* USE_AO */ | |
309 _exit(0); | |
10074 | 310 } else { |
311 gaim_timeout_add(PLAY_SOUND_TIMEOUT, expire_old_child, GINT_TO_POINTER(pid)); | |
5684 | 312 } |
313 #else /* USE_NAS_AUDIO || USE_AO */ | |
314 gdk_beep(); | |
315 return; | |
316 #endif /* USE_NAS_AUDIO || USE_AO */ | |
317 #else /* _WIN32 */ | |
318 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename); | |
319 | |
320 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) | |
321 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); | |
322 #endif /* _WIN32 */ | |
323 } | |
324 | |
325 static void gaim_gtk_sound_play_event(GaimSoundEventID event) | |
326 { | |
327 char *enable_pref; | |
328 char *file_pref; | |
329 | |
330 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) | |
331 return; | |
332 | |
333 if (event >= GAIM_NUM_SOUNDS) { | |
334 gaim_debug(GAIM_DEBUG_MISC, "sound", | |
335 "got request for unknown sound: %d\n", event); | |
336 return; | |
337 } | |
338 | |
339 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s", | |
340 sounds[event].pref); | |
341 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref); | |
342 | |
343 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
344 if (gaim_prefs_get_bool(enable_pref)) { | |
345 char *filename = g_strdup(gaim_prefs_get_string(file_pref)); | |
346 if(!filename || !strlen(filename)) { | |
347 if(filename) g_free(filename); | |
348 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); | |
349 } | |
350 | |
351 gaim_sound_play_file(filename); | |
352 g_free(filename); | |
353 } | |
354 | |
355 g_free(enable_pref); | |
356 g_free(file_pref); | |
357 } | |
358 | |
359 static GaimSoundUiOps sound_ui_ops = | |
360 { | |
361 gaim_gtk_sound_init, | |
362 gaim_gtk_sound_shutdown, | |
363 gaim_gtk_sound_play_file, | |
364 gaim_gtk_sound_play_event | |
365 }; | |
366 | |
7035
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
367 GaimSoundUiOps * |
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
368 gaim_gtk_sound_get_ui_ops(void) |
5684 | 369 { |
370 return &sound_ui_ops; | |
371 } | |
372 | |
373 | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
374 static void _pref_sound_method_changed(const char *name, GaimPrefType type, |
5684 | 375 gpointer val, gpointer data) { |
6778 | 376 if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method")) |
377 return; | |
378 | |
6199 | 379 sound_initialized = TRUE; |
380 | |
5684 | 381 #ifdef USE_AO |
382 ao_driver = -1; | |
383 | |
384 if(!strcmp(val, "esd")) | |
385 ao_driver = ao_driver_id("esd"); | |
386 else if(!strcmp(val, "arts")) | |
387 ao_driver = ao_driver_id("arts"); | |
388 else if(!strcmp(val, "automatic")) | |
389 ao_driver = ao_default_driver_id(); | |
390 | |
391 if(ao_driver != -1) { | |
392 ao_info *info = ao_driver_info(ao_driver); | |
393 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
394 "Sound output driver loaded: %s\n", info->name); | |
395 } | |
396 #endif /* USE_AO */ | |
397 #ifdef USE_NAS | |
398 if (!strcmp(val, "nas")) | |
399 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
400 "Sound output driver loaded: NAS output\n"); | |
401 #endif /* USE_NAS */ | |
402 } | |
403 | |
404 #ifdef USE_NAS_AUDIO | |
405 static gboolean play_file_nas(const char *filename) | |
406 { | |
407 AuServer *nas_serv; | |
408 gboolean ret = FALSE; | |
409 | |
410 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) { | |
411 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100); | |
412 AuCloseServer(nas_serv); | |
413 } | |
414 | |
415 return ret; | |
416 } | |
417 | |
418 #endif /* USE_NAS_AUDIO */ | |
419 | |
420 const char *gaim_gtk_sound_get_event_option(GaimSoundEventID event) | |
421 { | |
422 if(event >= GAIM_NUM_SOUNDS) | |
423 return 0; | |
424 | |
425 return sounds[event].pref; | |
426 } | |
427 | |
428 char *gaim_gtk_sound_get_event_label(GaimSoundEventID event) | |
429 { | |
430 if(event >= GAIM_NUM_SOUNDS) | |
431 return NULL; | |
432 | |
433 return sounds[event].label; | |
434 } | |
10087 | 435 |
436 void *gaim_gtk_sound_get_handle() | |
437 { | |
438 static int handle; | |
439 | |
440 return &handle; | |
441 } |