Mercurial > pidgin
annotate src/gtksound.c @ 7460:3973a09525b3
[gaim-migrate @ 8073]
This should fix the missing prefs for the buddy pounce sound.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 08 Nov 2003 05:52:39 +0000 |
parents | feb3d21a7794 |
children | a259e244e44f |
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 | |
177 command = g_strdup_printf(sound_cmd, filename); | |
178 | |
179 if(!g_spawn_command_line_async(command, &error)) { | |
180 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
181 gaim_notify_error(NULL, NULL, tmp, NULL); | |
182 g_free(tmp); | |
183 g_error_free(error); | |
184 } | |
185 | |
186 g_free(command); | |
187 return; | |
188 } | |
189 #if defined(USE_NAS_AUDIO) || defined(USE_AO) | |
190 pid = fork(); | |
191 if (pid < 0) | |
192 return; | |
193 else if (pid == 0) { | |
194 #ifdef USE_NAS_AUDIO | |
195 if (!strcmp(method, "nas")) { | |
196 if (play_file_nas(filename)) | |
197 _exit(0); | |
198 } | |
199 #endif /* USE_NAS_AUDIO */ | |
200 | |
201 #ifdef USE_AO | |
202 file = afOpenFile(filename, "rb", NULL); | |
203 if(file) { | |
204 ao_device *device; | |
205 ao_sample_format format; | |
206 int in_fmt; | |
207 int bytes_per_frame; | |
208 | |
209 format.rate = afGetRate(file, AF_DEFAULT_TRACK); | |
210 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
211 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
212 &format.bits); | |
213 | |
214 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll | |
215 * let libaudiofile make them a bit better for us */ | |
216 if(format.bits == 8) | |
217 format.bits = 16; | |
218 | |
219 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, | |
220 AF_SAMPFMT_TWOSCOMP, format.bits); | |
221 | |
222 #if __BYTE_ORDER == __BIG_ENDIAN | |
223 format.byte_format = AO_FMT_BIG; | |
224 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
225 AF_BYTEORDER_BIGENDIAN); | |
226 #elif __BYTE_ORDER == __LITTLE_ENDIAN | |
227 format.byte_format = AO_FMT_LITTLE; | |
228 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
229 AF_BYTEORDER_LITTLEENDIAN); | |
230 #endif | |
231 | |
232 bytes_per_frame = format.bits * format.channels / 8; | |
233 | |
234 device = ao_open_live(ao_driver, &format, NULL); | |
235 | |
236 if(device) { | |
237 int frames_read; | |
238 char buf[4096]; | |
239 int buf_frames = sizeof(buf) / bytes_per_frame; | |
240 | |
241 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
242 buf, buf_frames))) { | |
243 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
244 break; | |
245 } | |
246 ao_close(device); | |
247 } | |
248 afCloseFile(file); | |
249 } | |
250 ao_shutdown(); | |
251 #endif /* USE_AO */ | |
252 _exit(0); | |
253 } | |
254 #else /* USE_NAS_AUDIO || USE_AO */ | |
255 gdk_beep(); | |
256 return; | |
257 #endif /* USE_NAS_AUDIO || USE_AO */ | |
258 #else /* _WIN32 */ | |
259 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename); | |
260 | |
261 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) | |
262 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); | |
263 #endif /* _WIN32 */ | |
264 } | |
265 | |
266 static void gaim_gtk_sound_play_event(GaimSoundEventID event) | |
267 { | |
268 char *enable_pref; | |
269 char *file_pref; | |
270 | |
271 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) | |
272 return; | |
273 | |
274 if (event >= GAIM_NUM_SOUNDS) { | |
275 gaim_debug(GAIM_DEBUG_MISC, "sound", | |
276 "got request for unknown sound: %d\n", event); | |
277 return; | |
278 } | |
279 | |
280 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s", | |
281 sounds[event].pref); | |
282 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref); | |
283 | |
284 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
285 if (gaim_prefs_get_bool(enable_pref)) { | |
286 char *filename = g_strdup(gaim_prefs_get_string(file_pref)); | |
287 if(!filename || !strlen(filename)) { | |
288 if(filename) g_free(filename); | |
289 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); | |
290 } | |
291 | |
292 gaim_sound_play_file(filename); | |
293 g_free(filename); | |
294 } | |
295 | |
296 g_free(enable_pref); | |
297 g_free(file_pref); | |
298 } | |
299 | |
300 static GaimSoundUiOps sound_ui_ops = | |
301 { | |
302 gaim_gtk_sound_init, | |
303 gaim_gtk_sound_shutdown, | |
304 gaim_gtk_sound_play_file, | |
305 gaim_gtk_sound_play_event | |
306 }; | |
307 | |
7035
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
308 GaimSoundUiOps * |
feb3d21a7794
[gaim-migrate @ 7598]
Christian Hammond <chipx86@chipx86.com>
parents:
6778
diff
changeset
|
309 gaim_gtk_sound_get_ui_ops(void) |
5684 | 310 { |
311 return &sound_ui_ops; | |
312 } | |
313 | |
314 | |
5794
5e93fc46d1af
[gaim-migrate @ 6219]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
315 static void _pref_sound_method_changed(const char *name, GaimPrefType type, |
5684 | 316 gpointer val, gpointer data) { |
6778 | 317 if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method")) |
318 return; | |
319 | |
6199 | 320 sound_initialized = TRUE; |
321 | |
5684 | 322 #ifdef USE_AO |
323 ao_driver = -1; | |
324 | |
325 if(!strcmp(val, "esd")) | |
326 ao_driver = ao_driver_id("esd"); | |
327 else if(!strcmp(val, "arts")) | |
328 ao_driver = ao_driver_id("arts"); | |
329 else if(!strcmp(val, "automatic")) | |
330 ao_driver = ao_default_driver_id(); | |
331 | |
332 if(ao_driver != -1) { | |
333 ao_info *info = ao_driver_info(ao_driver); | |
334 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
335 "Sound output driver loaded: %s\n", info->name); | |
336 } | |
337 #endif /* USE_AO */ | |
338 #ifdef USE_NAS | |
339 if (!strcmp(val, "nas")) | |
340 gaim_debug(GAIM_DEBUG_INFO, "sound", | |
341 "Sound output driver loaded: NAS output\n"); | |
342 #endif /* USE_NAS */ | |
343 } | |
344 | |
345 #ifdef USE_NAS_AUDIO | |
346 static gboolean play_file_nas(const char *filename) | |
347 { | |
348 AuServer *nas_serv; | |
349 gboolean ret = FALSE; | |
350 | |
351 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) { | |
352 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100); | |
353 AuCloseServer(nas_serv); | |
354 } | |
355 | |
356 return ret; | |
357 } | |
358 | |
359 #endif /* USE_NAS_AUDIO */ | |
360 | |
361 void gaim_gtk_sound_set_mute(gboolean mute) | |
362 { | |
363 mute_sounds = mute; | |
364 } | |
365 | |
366 gboolean gaim_gtk_sound_get_mute() | |
367 { | |
368 return mute_sounds; | |
369 } | |
370 | |
371 void gaim_gtk_sound_set_login_mute(gboolean mute) | |
372 { | |
373 mute_login_sounds = mute; | |
374 } | |
375 | |
376 const char *gaim_gtk_sound_get_event_option(GaimSoundEventID event) | |
377 { | |
378 if(event >= GAIM_NUM_SOUNDS) | |
379 return 0; | |
380 | |
381 return sounds[event].pref; | |
382 } | |
383 | |
384 char *gaim_gtk_sound_get_event_label(GaimSoundEventID event) | |
385 { | |
386 if(event >= GAIM_NUM_SOUNDS) | |
387 return NULL; | |
388 | |
389 return sounds[event].label; | |
390 } | |
391 |