comparison pidgin/gtksound.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children d75099d2567e
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /*
2 * @file gtksound.c GTK+ Sound
3 * @ingroup gtkui
4 *
5 * gaim
6 *
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.
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 */
26 #include "internal.h"
27 #include "gtkgaim.h"
28
29 #ifdef _WIN32
30 #include <windows.h>
31 #include <mmsystem.h>
32 #endif
33
34 #ifdef USE_GSTREAMER
35 # include <gst/gst.h>
36 #endif /* USE_GSTREAMER */
37
38 #include "debug.h"
39 #include "notify.h"
40 #include "prefs.h"
41 #include "sound.h"
42 #include "util.h"
43
44 #include "gtkconv.h"
45 #include "gtksound.h"
46
47 struct gaim_sound_event {
48 char *label;
49 char *pref;
50 char *def;
51 };
52
53 #define PLAY_SOUND_TIMEOUT 15000
54
55 static guint mute_login_sounds_timeout = 0;
56 static gboolean mute_login_sounds = FALSE;
57
58 #ifdef USE_GSTREAMER
59 static gboolean gst_init_failed;
60 #endif /* USE_GSTREAMER */
61
62 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = {
63 {N_("Buddy logs in"), "login", "login.wav"},
64 {N_("Buddy logs out"), "logout", "logout.wav"},
65 {N_("Message received"), "im_recv", "receive.wav"},
66 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"},
67 {N_("Message sent"), "send_im", "send.wav"},
68 {N_("Person enters chat"), "join_chat", "login.wav"},
69 {N_("Person leaves chat"), "left_chat", "logout.wav"},
70 {N_("You talk in chat"), "send_chat_msg", "send.wav"},
71 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"},
72 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
73 {NULL, "pounce_default", "alert.wav"},
74 {N_("Someone says your screen name in chat"), "nick_said", "alert.wav"}
75 };
76
77 static gboolean
78 unmute_login_sounds_cb(gpointer data)
79 {
80 mute_login_sounds = FALSE;
81 mute_login_sounds_timeout = 0;
82 return FALSE;
83 }
84
85 static gboolean
86 chat_nick_matches_name(GaimConversation *conv, const char *aname)
87 {
88 GaimConvChat *chat = NULL;
89 char *nick = NULL;
90 char *name = NULL;
91 gboolean ret = FALSE;
92 chat = gaim_conversation_get_chat_data(conv);
93
94 if (chat==NULL)
95 return ret;
96
97 nick = g_strdup(gaim_normalize(conv->account, chat->nick));
98 name = g_strdup(gaim_normalize(conv->account, aname));
99
100 if (g_utf8_collate(nick, name) == 0)
101 ret = TRUE;
102
103 g_free(nick);
104 g_free(name);
105
106 return ret;
107 }
108
109 /*
110 * play a sound event for a conversation, honoring make_sound flag
111 * of conversation and checking for focus if conv_focus pref is set
112 */
113 static void
114 play_conv_event(GaimConversation *conv, GaimSoundEventID event)
115 {
116 /* If we should not play the sound for some reason, then exit early */
117 if (conv != NULL)
118 {
119 GaimGtkConversation *gtkconv;
120 GaimGtkWindow *win;
121 gboolean has_focus;
122
123 gtkconv = GAIM_GTK_CONVERSATION(conv);
124 win = gtkconv->win;
125
126 has_focus = gaim_conversation_has_focus(conv);
127
128 if (!gtkconv->make_sound ||
129 (has_focus && !gaim_prefs_get_bool("/gaim/gtk/sound/conv_focus")))
130 {
131 return;
132 }
133 }
134
135 gaim_sound_play_event(event, conv ? gaim_conversation_get_account(conv) : NULL);
136 }
137
138 static void
139 buddy_state_cb(GaimBuddy *buddy, GaimSoundEventID event)
140 {
141 gaim_sound_play_event(event, gaim_buddy_get_account(buddy));
142 }
143
144 static void
145 im_msg_received_cb(GaimAccount *account, char *sender,
146 char *message, GaimConversation *conv,
147 GaimMessageFlags flags, GaimSoundEventID event)
148 {
149 if (flags & GAIM_MESSAGE_DELAYED)
150 return;
151
152 if (conv==NULL)
153 gaim_sound_play_event(GAIM_SOUND_FIRST_RECEIVE, account);
154 else
155 play_conv_event(conv, event);
156 }
157
158 static void
159 im_msg_sent_cb(GaimAccount *account, const char *receiver,
160 const char *message, GaimSoundEventID event)
161 {
162 GaimConversation *conv = gaim_find_conversation_with_account(
163 GAIM_CONV_TYPE_ANY, receiver, account);
164 play_conv_event(conv, event);
165 }
166
167 static void
168 chat_buddy_join_cb(GaimConversation *conv, const char *name,
169 GaimConvChatBuddyFlags flags, gboolean new_arrival,
170 GaimSoundEventID event)
171 {
172 if (new_arrival && !chat_nick_matches_name(conv, name))
173 play_conv_event(conv, event);
174 }
175
176 static void
177 chat_buddy_left_cb(GaimConversation *conv, const char *name,
178 const char *reason, GaimSoundEventID event)
179 {
180 if (!chat_nick_matches_name(conv, name))
181 play_conv_event(conv, event);
182 }
183
184 static void
185 chat_msg_sent_cb(GaimAccount *account, const char *message,
186 int id, GaimSoundEventID event)
187 {
188 GaimConnection *conn = gaim_account_get_connection(account);
189 GaimConversation *conv = NULL;
190
191 if (conn!=NULL)
192 conv = gaim_find_chat(conn,id);
193
194 play_conv_event(conv, event);
195 }
196
197 static void
198 chat_msg_received_cb(GaimAccount *account, char *sender,
199 char *message, GaimConversation *conv,
200 GaimMessageFlags flags, GaimSoundEventID event)
201 {
202 GaimConvChat *chat;
203
204 if (flags & GAIM_MESSAGE_DELAYED)
205 return;
206
207 chat = gaim_conversation_get_chat_data(conv);
208 g_return_if_fail(chat != NULL);
209
210 if (gaim_conv_chat_is_user_ignored(chat, sender))
211 return;
212
213 if (chat_nick_matches_name(conv, sender))
214 return;
215
216 if (flags & GAIM_MESSAGE_NICK || gaim_utf8_has_word(message, chat->nick))
217 play_conv_event(conv, GAIM_SOUND_CHAT_NICK);
218 else
219 play_conv_event(conv, event);
220 }
221
222 /*
223 * We mute sounds for the 10 seconds after you log in so that
224 * you don't get flooded with sounds when the blist shows all
225 * your buddies logging in.
226 */
227 static void
228 account_signon_cb(GaimConnection *gc, gpointer data)
229 {
230 if (mute_login_sounds_timeout != 0)
231 g_source_remove(mute_login_sounds_timeout);
232 mute_login_sounds = TRUE;
233 mute_login_sounds_timeout = gaim_timeout_add(10000, unmute_login_sounds_cb, NULL);
234 }
235
236 const char *
237 gaim_gtk_sound_get_event_option(GaimSoundEventID event)
238 {
239 if(event >= GAIM_NUM_SOUNDS)
240 return 0;
241
242 return sounds[event].pref;
243 }
244
245 const char *
246 gaim_gtk_sound_get_event_label(GaimSoundEventID event)
247 {
248 if(event >= GAIM_NUM_SOUNDS)
249 return NULL;
250
251 return sounds[event].label;
252 }
253
254 void *
255 gaim_gtk_sound_get_handle()
256 {
257 static int handle;
258
259 return &handle;
260 }
261
262 static void
263 gaim_gtk_sound_init(void)
264 {
265 void *gtk_sound_handle = gaim_gtk_sound_get_handle();
266 void *blist_handle = gaim_blist_get_handle();
267 void *conv_handle = gaim_conversations_get_handle();
268 #ifdef USE_GSTREAMER
269 GError *error = NULL;
270 #endif
271
272 gaim_signal_connect(gaim_connections_get_handle(), "signed-on",
273 gtk_sound_handle, GAIM_CALLBACK(account_signon_cb),
274 NULL);
275
276 gaim_prefs_add_none("/gaim/gtk/sound");
277 gaim_prefs_add_none("/gaim/gtk/sound/enabled");
278 gaim_prefs_add_none("/gaim/gtk/sound/file");
279 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/login", TRUE);
280 gaim_prefs_add_path("/gaim/gtk/sound/file/login", "");
281 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/logout", TRUE);
282 gaim_prefs_add_path("/gaim/gtk/sound/file/logout", "");
283 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/im_recv", TRUE);
284 gaim_prefs_add_path("/gaim/gtk/sound/file/im_recv", "");
285 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/first_im_recv", FALSE);
286 gaim_prefs_add_path("/gaim/gtk/sound/file/first_im_recv", "");
287 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_im", TRUE);
288 gaim_prefs_add_path("/gaim/gtk/sound/file/send_im", "");
289 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/join_chat", FALSE);
290 gaim_prefs_add_path("/gaim/gtk/sound/file/join_chat", "");
291 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/left_chat", FALSE);
292 gaim_prefs_add_path("/gaim/gtk/sound/file/left_chat", "");
293 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/send_chat_msg", FALSE);
294 gaim_prefs_add_path("/gaim/gtk/sound/file/send_chat_msg", "");
295 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/chat_msg_recv", FALSE);
296 gaim_prefs_add_path("/gaim/gtk/sound/file/chat_msg_recv", "");
297 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/nick_said", FALSE);
298 gaim_prefs_add_path("/gaim/gtk/sound/file/nick_said", "");
299 gaim_prefs_add_bool("/gaim/gtk/sound/enabled/pounce_default", TRUE);
300 gaim_prefs_add_path("/gaim/gtk/sound/file/pounce_default", "");
301 gaim_prefs_add_bool("/gaim/gtk/sound/conv_focus", TRUE);
302 gaim_prefs_add_bool("/gaim/gtk/sound/mute", FALSE);
303 gaim_prefs_add_path("/gaim/gtk/sound/command", "");
304 gaim_prefs_add_string("/gaim/gtk/sound/method", "automatic");
305 gaim_prefs_add_int("/gaim/gtk/sound/volume", 50);
306
307 #ifdef USE_GSTREAMER
308 gaim_debug_info("sound", "Initializing sound output drivers.\n");
309 if ((gst_init_failed = !gst_init_check(NULL, NULL, &error))) {
310 gaim_notify_error(NULL, _("GStreamer Failure"),
311 _("GStreamer failed to initialize."),
312 error ? error->message : "");
313 if (error) {
314 g_error_free(error);
315 error = NULL;
316 }
317 }
318 #endif /* USE_GSTREAMER */
319
320 gaim_signal_connect(blist_handle, "buddy-signed-on",
321 gtk_sound_handle, GAIM_CALLBACK(buddy_state_cb),
322 GINT_TO_POINTER(GAIM_SOUND_BUDDY_ARRIVE));
323 gaim_signal_connect(blist_handle, "buddy-signed-off",
324 gtk_sound_handle, GAIM_CALLBACK(buddy_state_cb),
325 GINT_TO_POINTER(GAIM_SOUND_BUDDY_LEAVE));
326 gaim_signal_connect(conv_handle, "received-im-msg",
327 gtk_sound_handle, GAIM_CALLBACK(im_msg_received_cb),
328 GINT_TO_POINTER(GAIM_SOUND_RECEIVE));
329 gaim_signal_connect(conv_handle, "sent-im-msg",
330 gtk_sound_handle, GAIM_CALLBACK(im_msg_sent_cb),
331 GINT_TO_POINTER(GAIM_SOUND_SEND));
332 gaim_signal_connect(conv_handle, "chat-buddy-joined",
333 gtk_sound_handle, GAIM_CALLBACK(chat_buddy_join_cb),
334 GINT_TO_POINTER(GAIM_SOUND_CHAT_JOIN));
335 gaim_signal_connect(conv_handle, "chat-buddy-left",
336 gtk_sound_handle, GAIM_CALLBACK(chat_buddy_left_cb),
337 GINT_TO_POINTER(GAIM_SOUND_CHAT_LEAVE));
338 gaim_signal_connect(conv_handle, "sent-chat-msg",
339 gtk_sound_handle, GAIM_CALLBACK(chat_msg_sent_cb),
340 GINT_TO_POINTER(GAIM_SOUND_CHAT_YOU_SAY));
341 gaim_signal_connect(conv_handle, "received-chat-msg",
342 gtk_sound_handle, GAIM_CALLBACK(chat_msg_received_cb),
343 GINT_TO_POINTER(GAIM_SOUND_CHAT_SAY));
344 }
345
346 static void
347 gaim_gtk_sound_uninit(void)
348 {
349 #ifdef USE_GSTREAMER
350 if (!gst_init_failed)
351 gst_deinit();
352 #endif
353
354 gaim_signals_disconnect_by_handle(gaim_gtk_sound_get_handle());
355 }
356
357 #ifdef USE_GSTREAMER
358 static gboolean
359 bus_call (GstBus *bus,
360 GstMessage *msg,
361 gpointer data)
362 {
363 GstElement *play = data;
364 GError *err = NULL;
365
366 switch (GST_MESSAGE_TYPE (msg)) {
367 case GST_MESSAGE_EOS:
368 gst_element_set_state(play, GST_STATE_NULL);
369 gst_object_unref(GST_OBJECT(play));
370 break;
371 case GST_MESSAGE_ERROR:
372 gst_message_parse_error(msg, &err, NULL);
373 gaim_debug_error("gstreamer", err->message);
374 g_error_free(err);
375 break;
376 case GST_MESSAGE_WARNING:
377 gst_message_parse_warning(msg, &err, NULL);
378 gaim_debug_warning("gstreamer", err->message);
379 g_error_free(err);
380 break;
381 default:
382 break;
383 }
384 return TRUE;
385 }
386 #endif
387
388 static void
389 gaim_gtk_sound_play_file(const char *filename)
390 {
391 const char *method;
392 #ifdef USE_GSTREAMER
393 float volume;
394 char *uri;
395 GstElement *sink = NULL;
396 GstElement *play = NULL;
397 GstBus *bus = NULL;
398 #endif
399
400 if (gaim_prefs_get_bool("/gaim/gtk/sound/mute"))
401 return;
402
403 method = gaim_prefs_get_string("/gaim/gtk/sound/method");
404
405 if (!strcmp(method, "none")) {
406 return;
407 } else if (!strcmp(method, "beep")) {
408 gdk_beep();
409 return;
410 }
411
412 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
413 gaim_debug_error("gtksound", "sound file (%s) does not exist.\n", filename);
414 return;
415 }
416
417 #ifndef _WIN32
418 if (!strcmp(method, "custom")) {
419 const char *sound_cmd;
420 char *command;
421 GError *error = NULL;
422
423 sound_cmd = gaim_prefs_get_path("/gaim/gtk/sound/command");
424
425 if (!sound_cmd || *sound_cmd == '\0') {
426 gaim_debug_error("gtksound",
427 "'Command' sound method has been chosen, "
428 "but no command has been set.");
429 return;
430 }
431
432 if(strstr(sound_cmd, "%s"))
433 command = gaim_strreplace(sound_cmd, "%s", filename);
434 else
435 command = g_strdup_printf("%s %s", sound_cmd, filename);
436
437 if(!g_spawn_command_line_async(command, &error)) {
438 gaim_debug_error("gtksound", "sound command could not be launched: %s\n", error->message);
439 g_free(command);
440 g_error_free(error);
441 }
442
443 g_free(command);
444 return;
445 }
446 #ifdef USE_GSTREAMER
447 if (gst_init_failed) /* Perhaps do gdk_beep instead? */
448 return;
449 volume = (float)(CLAMP(gaim_prefs_get_int("/gaim/gtk/sound/volume"),0,100)) / 50;
450 if (!strcmp(method, "automatic")) {
451 if (gaim_running_gnome()) {
452 sink = gst_element_factory_make("gconfaudiosink", "sink");
453 }
454 if (!sink)
455 sink = gst_element_factory_make("autoaudiosink", "sink");
456 if (!sink) {
457 gaim_debug_error("sound", "Unable to create GStreamer audiosink.\n");
458 return;
459 }
460 } else if (!strcmp(method, "esd")) {
461 sink = gst_element_factory_make("esdsink", "sink");
462 if (!sink) {
463 gaim_debug_error("sound", "Unable to create GStreamer audiosink.\n");
464 return;
465 }
466 } else {
467 gaim_debug_error("sound", "Unknown sound method '%s'\n", method);
468 return;
469 }
470
471 play = gst_element_factory_make("playbin", "play");
472
473 uri = g_strdup_printf("file://%s", filename);
474
475 g_object_set(G_OBJECT(play), "uri", uri,
476 "volume", volume,
477 "audio-sink", sink, NULL);
478
479 bus = gst_pipeline_get_bus(GST_PIPELINE(play));
480 gst_bus_add_watch(bus, bus_call, play);
481
482 gst_element_set_state(play, GST_STATE_PLAYING);
483
484 gst_object_unref(bus);
485 g_free(uri);
486
487 #else /* USE_GSTREAMER */
488 gdk_beep();
489 return;
490 #endif /* USE_GSTREAMER */
491 #else /* _WIN32 */
492 gaim_debug_info("sound", "Playing %s\n", filename);
493
494 if (G_WIN32_HAVE_WIDECHAR_API ()) {
495 wchar_t *wc_filename = g_utf8_to_utf16(filename,
496 -1, NULL, NULL, NULL);
497 if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME))
498 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n");
499 g_free(wc_filename);
500 } else {
501 char *l_filename = g_locale_from_utf8(filename,
502 -1, NULL, NULL, NULL);
503 if (!PlaySoundA(l_filename, NULL, SND_ASYNC | SND_FILENAME))
504 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n");
505 g_free(l_filename);
506 }
507 #endif /* _WIN32 */
508 }
509
510 static void
511 gaim_gtk_sound_play_event(GaimSoundEventID event)
512 {
513 char *enable_pref;
514 char *file_pref;
515
516 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
517 return;
518
519 if (event >= GAIM_NUM_SOUNDS) {
520 gaim_debug_error("sound", "got request for unknown sound: %d\n", event);
521 return;
522 }
523
524 enable_pref = g_strdup_printf("/gaim/gtk/sound/enabled/%s",
525 sounds[event].pref);
526 file_pref = g_strdup_printf("/gaim/gtk/sound/file/%s", sounds[event].pref);
527
528 /* check NULL for sounds that don't have an option, ie buddy pounce */
529 if (gaim_prefs_get_bool(enable_pref)) {
530 char *filename = g_strdup(gaim_prefs_get_path(file_pref));
531 if(!filename || !strlen(filename)) {
532 g_free(filename);
533 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL);
534 }
535
536 gaim_sound_play_file(filename, NULL);
537 g_free(filename);
538 }
539
540 g_free(enable_pref);
541 g_free(file_pref);
542 }
543
544 static GaimSoundUiOps sound_ui_ops =
545 {
546 gaim_gtk_sound_init,
547 gaim_gtk_sound_uninit,
548 gaim_gtk_sound_play_file,
549 gaim_gtk_sound_play_event
550 };
551
552 GaimSoundUiOps *
553 gaim_gtk_sound_get_ui_ops(void)
554 {
555 return &sound_ui_ops;
556 }