comparison src/gtksound.c @ 5684:b61520e71679

[gaim-migrate @ 6104] sound is now really core/ui split. committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Tue, 03 Jun 2003 03:33:20 +0000
parents
children 5e93fc46d1af
comparison
equal deleted inserted replaced
5683:9befba33f7c8 5684:b61520e71679
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 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #ifdef HAVE_ENDIAN_H
28 #include <endian.h>
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <glib.h>
34
35 #ifndef _WIN32
36 #include <unistd.h>
37 #else
38 #include <windows.h>
39 #include <mmsystem.h>
40 #endif
41
42 #ifdef USE_AO
43 #include <ao/ao.h>
44 #include <audiofile.h>
45 #endif /* USE_AO */
46
47 #ifdef USE_NAS_AUDIO
48 #include <audio/audiolib.h>
49 #include <audio/soundlib.h>
50 #endif /* USE_NAS_AUDIO */
51
52 #include "gaim.h" /* for gettext stuff */
53 #include "sound.h"
54 #include "gtksound.h"
55 #include "prefs.h"
56
57 #ifdef _WIN32
58 #include "win32dep.h"
59 #endif
60
61
62 struct gaim_sound_event {
63 char *label;
64 char *pref;
65 char *def;
66 };
67
68
69
70 static gboolean mute_login_sounds = FALSE;
71 static gboolean mute_sounds = FALSE;
72 static char *sound_cmd = NULL;
73
74 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = {
75 {N_("Buddy logs in"), "login", "arrive.wav"},
76 {N_("Buddy logs out"), "logout", "leave.wav"},
77 {N_("Message received"), "im_recv", "receive.wav"},
78 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"},
79 {N_("Message sent"), "send_im", "send.wav"},
80 {N_("Person enters chat"), "join_chat", "arrive.wav"},
81 {N_("Person leaves chat"), "left_chat", "leave.wav"},
82 {N_("You talk in chat"), "send_chat_msg", "send.wav"},
83 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"},
84 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
85 {NULL, "pounce_default", "redalert.wav"},
86 {N_("Someone says your name in chat"), "nick_said", "redalert.wav"}
87 };
88
89 #ifdef USE_AO
90 static int ao_driver = -1;
91 #endif /* USE_AO */
92
93 static void __pref_sound_method_changed(const char *name, GaimPrefType type,
94 gpointer val, gpointer data);
95
96 static void gaim_gtk_sound_init(void)
97 {
98 gaim_prefs_add_none("/gaim/gtk/sound");
99 gaim_prefs_add_none("/gaim/gtk/sound/enabled");
100 gaim_prefs_add_none("/gaim/gtk/sound/file");
101 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE);
102 gaim_prefs_add_string("/gaim/gtk/sound/file/login", "");
103 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE);
104 gaim_prefs_add_string("/gaim/gtk/sound/file/logout", "");
105 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE);
106 gaim_prefs_add_string("/gaim/gtk/sound/file/im_recv", "");
107 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE);
108 gaim_prefs_add_string("/gaim/gtk/sound/file/first_im_recv", "");
109 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE);
110 gaim_prefs_add_string("/gaim/gtk/sound/file/send_im", "");
111 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE);
112 gaim_prefs_add_string("/gaim/gtk/sound/file/join_chat", "");
113 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE);
114 gaim_prefs_add_string("/gaim/gtk/sound/file/left_chat", "");
115 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE);
116 gaim_prefs_add_string("/gaim/gtk/sound/file/send_chat_msg", "");
117 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE);
118 gaim_prefs_add_string("/gaim/gtk/sound/file/chat_msg_recv", "");
119 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE);
120 gaim_prefs_add_string("/gaim/gtk/sound/file/nick_said", "");
121 gaim_prefs_add_bool("/gaim/gtk/sound/silent_signon", TRUE);
122 gaim_prefs_add_string("/gaim/gtk/sound/command", "");
123 gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic");
124
125 #ifdef USE_AO
126 gaim_debug(GAIM_DEBUG_INFO, "sound",
127 "Initializing sound output drivers.\n");
128 ao_initialize();
129 #endif /* USE_AO */
130
131 gaim_prefs_connect_callback("/gaim/gtk/sound/method",
132 __pref_sound_method_changed, NULL);
133 gaim_prefs_trigger_callback("/gaim/gtk/sound/method");
134 }
135
136
137 static void gaim_gtk_sound_shutdown(void)
138 {
139 #ifdef USE_AO
140 ao_shutdown();
141 #endif
142 }
143
144 static void gaim_gtk_sound_play_file(const char *filename)
145 {
146 const char *method;
147 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
148 pid_t pid;
149 #ifdef USE_AO
150 AFfilehandle file;
151 #endif
152 #endif
153
154 if (mute_sounds)
155 return;
156
157 method = gaim_prefs_get_string("/gaim/gtk/sound/method");
158
159 if (!strcmp(method, "beep")) {
160 gdk_beep();
161 return;
162 }
163
164 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
165 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename);
166 gaim_notify_error(NULL, NULL, tmp, NULL);
167 g_free(tmp);
168 return;
169 }
170
171 #ifndef _WIN32
172 if (!strcmp(method, "custom")) {
173 const char *sound_cmd;
174 char *command;
175 GError *error = NULL;
176
177 sound_cmd = gaim_prefs_get_string("/gaim/gtk/sound/command");
178
179 if (!sound_cmd || *sound_cmd == '\0') {
180 gaim_notify_error(NULL, NULL,
181 _("Unable to play sound because the "
182 "'Command' sound method has been chosen, "
183 "but no command has been set."), NULL);
184 return;
185 }
186
187 command = g_strdup_printf(sound_cmd, filename);
188
189 if(!g_spawn_command_line_async(command, &error)) {
190 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message);
191 gaim_notify_error(NULL, NULL, tmp, NULL);
192 g_free(tmp);
193 g_error_free(error);
194 }
195
196 g_free(command);
197 return;
198 }
199 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
200 pid = fork();
201 if (pid < 0)
202 return;
203 else if (pid == 0) {
204 #ifdef USE_NAS_AUDIO
205 if (!strcmp(method, "nas")) {
206 if (play_file_nas(filename))
207 _exit(0);
208 }
209 #endif /* USE_NAS_AUDIO */
210
211 #ifdef USE_AO
212 file = afOpenFile(filename, "rb", NULL);
213 if(file) {
214 ao_device *device;
215 ao_sample_format format;
216 int in_fmt;
217 int bytes_per_frame;
218
219 format.rate = afGetRate(file, AF_DEFAULT_TRACK);
220 format.channels = afGetChannels(file, AF_DEFAULT_TRACK);
221 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt,
222 &format.bits);
223
224 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll
225 * let libaudiofile make them a bit better for us */
226 if(format.bits == 8)
227 format.bits = 16;
228
229 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK,
230 AF_SAMPFMT_TWOSCOMP, format.bits);
231
232 #if __BYTE_ORDER == __BIG_ENDIAN
233 format.byte_format = AO_FMT_BIG;
234 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK,
235 AF_BYTEORDER_BIGENDIAN);
236 #elif __BYTE_ORDER == __LITTLE_ENDIAN
237 format.byte_format = AO_FMT_LITTLE;
238 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK,
239 AF_BYTEORDER_LITTLEENDIAN);
240 #endif
241
242 bytes_per_frame = format.bits * format.channels / 8;
243
244 device = ao_open_live(ao_driver, &format, NULL);
245
246 if(device) {
247 int frames_read;
248 char buf[4096];
249 int buf_frames = sizeof(buf) / bytes_per_frame;
250
251 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK,
252 buf, buf_frames))) {
253 if(!ao_play(device, buf, frames_read * bytes_per_frame))
254 break;
255 }
256 ao_close(device);
257 }
258 afCloseFile(file);
259 }
260 ao_shutdown();
261 #endif /* USE_AO */
262 _exit(0);
263 }
264 #else /* USE_NAS_AUDIO || USE_AO */
265 gdk_beep();
266 return;
267 #endif /* USE_NAS_AUDIO || USE_AO */
268 #else /* _WIN32 */
269 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename);
270
271 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME))
272 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n");
273 #endif /* _WIN32 */
274 }
275
276 static void gaim_gtk_sound_play_event(GaimSoundEventID event)
277 {
278 char *enable_pref;
279 char *file_pref;
280
281 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
282 return;
283
284 if (event >= GAIM_NUM_SOUNDS) {
285 gaim_debug(GAIM_DEBUG_MISC, "sound",
286 "got request for unknown sound: %d\n", event);
287 return;
288 }
289
290 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s",
291 sounds[event].pref);
292 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref);
293
294 /* check NULL for sounds that don't have an option, ie buddy pounce */
295 if (gaim_prefs_get_bool(enable_pref)) {
296 char *filename = g_strdup(gaim_prefs_get_string(file_pref));
297 if(!filename || !strlen(filename)) {
298 if(filename) g_free(filename);
299 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL);
300 }
301
302 gaim_sound_play_file(filename);
303 g_free(filename);
304 }
305
306 g_free(enable_pref);
307 g_free(file_pref);
308 }
309
310 static GaimSoundUiOps sound_ui_ops =
311 {
312 gaim_gtk_sound_init,
313 gaim_gtk_sound_shutdown,
314 gaim_gtk_sound_play_file,
315 gaim_gtk_sound_play_event
316 };
317
318 GaimSoundUiOps *gaim_get_gtk_sound_ui_ops(void)
319 {
320 return &sound_ui_ops;
321 }
322
323
324 static void __pref_sound_method_changed(const char *name, GaimPrefType type,
325 gpointer val, gpointer data) {
326 if(type != GAIM_PREF_STRING || strcmp(name, "/gaim/gtk/sound/method"))
327 return;
328 #ifdef USE_AO
329 ao_driver = -1;
330
331 if(!strcmp(val, "esd"))
332 ao_driver = ao_driver_id("esd");
333 else if(!strcmp(val, "arts"))
334 ao_driver = ao_driver_id("arts");
335 else if(!strcmp(val, "automatic"))
336 ao_driver = ao_default_driver_id();
337
338 if(ao_driver != -1) {
339 ao_info *info = ao_driver_info(ao_driver);
340 gaim_debug(GAIM_DEBUG_INFO, "sound",
341 "Sound output driver loaded: %s\n", info->name);
342 }
343 #endif /* USE_AO */
344 #ifdef USE_NAS
345 if (!strcmp(val, "nas"))
346 gaim_debug(GAIM_DEBUG_INFO, "sound",
347 "Sound output driver loaded: NAS output\n");
348 #endif /* USE_NAS */
349 }
350
351 #ifdef USE_NAS_AUDIO
352 static gboolean play_file_nas(const char *filename)
353 {
354 AuServer *nas_serv;
355 gboolean ret = FALSE;
356
357 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) {
358 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100);
359 AuCloseServer(nas_serv);
360 }
361
362 return ret;
363 }
364
365 #endif /* USE_NAS_AUDIO */
366
367 void gaim_gtk_sound_set_mute(gboolean mute)
368 {
369 mute_sounds = mute;
370 }
371
372 gboolean gaim_gtk_sound_get_mute()
373 {
374 return mute_sounds;
375 }
376
377 void gaim_gtk_sound_set_login_mute(gboolean mute)
378 {
379 mute_login_sounds = mute;
380 }
381
382 const char *gaim_gtk_sound_get_event_option(GaimSoundEventID event)
383 {
384 if(event >= GAIM_NUM_SOUNDS)
385 return 0;
386
387 return sounds[event].pref;
388 }
389
390 char *gaim_gtk_sound_get_event_label(GaimSoundEventID event)
391 {
392 if(event >= GAIM_NUM_SOUNDS)
393 return NULL;
394
395 return sounds[event].label;
396 }
397
398
399 void gaim_sound_set_command(const char *cmd)
400 {
401 if(sound_cmd)
402 g_free(sound_cmd);
403 if(strlen(cmd) > 0)
404 sound_cmd = g_strdup(cmd);
405 else
406 sound_cmd = NULL;
407 }
408
409 char *gaim_sound_get_command()
410 {
411 return sound_cmd;
412 }
413