3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
32 #endif /* USE_GSTREAMER */
38 #include "sound-theme.h"
39 #include "theme-manager.h"
45 struct pidgin_sound_event
{
51 static guint mute_login_sounds_timeout
= 0;
52 static gboolean mute_login_sounds
= FALSE
;
55 static gboolean gst_init_failed
;
56 #endif /* USE_GSTREAMER */
58 static const struct pidgin_sound_event sounds
[PURPLE_NUM_SOUNDS
] = {
59 {N_("Buddy logs in"), "login", "login.wav"},
60 {N_("Buddy logs out"), "logout", "logout.wav"},
61 {N_("Message received"), "im_recv", "receive.wav"},
62 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"},
63 {N_("Message sent"), "send_im", "send.wav"},
64 {N_("Person enters chat"), "join_chat", "login.wav"},
65 {N_("Person leaves chat"), "left_chat", "logout.wav"},
66 {N_("You talk in chat"), "send_chat_msg", "send.wav"},
67 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"},
68 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
69 {NULL
, "pounce_default", "alert.wav"},
70 {N_("Someone says your username in chat"), "nick_said", "alert.wav"},
71 {N_("Attention received"), "got_attention", "alert.wav"}
75 unmute_login_sounds_cb(gpointer data
)
77 mute_login_sounds
= FALSE
;
78 mute_login_sounds_timeout
= 0;
83 chat_nick_matches_name(PurpleChatConversation
*chat
, const char *aname
)
92 nick
= g_strdup(purple_normalize(purple_conversation_get_account(
93 PURPLE_CONVERSATION(chat
)), purple_chat_conversation_get_nick(chat
)));
94 name
= g_strdup(purple_normalize(purple_conversation_get_account(
95 PURPLE_CONVERSATION(chat
)), aname
));
97 if (g_utf8_collate(nick
, name
) == 0)
107 * play a sound event for a conversation, honoring make_sound flag
108 * of conversation and checking for focus if conv_focus pref is set
111 play_conv_event(PurpleConversation
*conv
, PurpleSoundEventID event
)
113 g_return_if_fail(event
< PURPLE_NUM_SOUNDS
);
115 /* If we should not play the sound for some reason, then exit early */
116 if (conv
!= NULL
&& PIDGIN_IS_PIDGIN_CONVERSATION(conv
))
118 PidginConversation
*gtkconv
;
121 gtkconv
= PIDGIN_CONVERSATION(conv
);
122 has_focus
= purple_conversation_has_focus(conv
);
124 if (!gtkconv
->make_sound
||
125 (has_focus
&& !purple_prefs_get_bool(PIDGIN_PREFS_ROOT
"/sound/conv_focus")))
131 purple_sound_play_event(event
, conv
? purple_conversation_get_account(conv
) : NULL
);
135 buddy_state_cb(PurpleBuddy
*buddy
, PurpleSoundEventID event
)
137 purple_sound_play_event(event
, purple_buddy_get_account(buddy
));
141 im_msg_received_cb(PurpleAccount
*account
, char *sender
,
142 char *message
, PurpleConversation
*conv
,
143 PurpleMessageFlags flags
, PurpleSoundEventID event
)
145 if (flags
& PURPLE_MESSAGE_DELAYED
|| flags
& PURPLE_MESSAGE_NOTIFY
)
149 purple_sound_play_event(PURPLE_SOUND_FIRST_RECEIVE
, account
);
151 play_conv_event(conv
, event
);
155 im_msg_sent_cb(PurpleAccount
*account
, PurpleMessage
*msg
,
156 PurpleSoundEventID event
)
158 PurpleConversation
*conv
= PURPLE_CONVERSATION(
159 purple_conversations_find_im_with_account(
160 purple_message_get_recipient(msg
), account
));
161 play_conv_event(conv
, event
);
165 chat_user_join_cb(PurpleChatConversation
*chat
, const char *name
,
166 PurpleChatUserFlags flags
, gboolean new_arrival
,
167 PurpleSoundEventID event
)
169 if (new_arrival
&& !chat_nick_matches_name(chat
, name
))
170 play_conv_event(PURPLE_CONVERSATION(chat
), event
);
174 chat_user_left_cb(PurpleChatConversation
*chat
, const char *name
,
175 const char *reason
, PurpleSoundEventID event
)
177 if (!chat_nick_matches_name(chat
, name
))
178 play_conv_event(PURPLE_CONVERSATION(chat
), event
);
182 chat_msg_sent_cb(PurpleAccount
*account
, PurpleMessage
*msg
, int id
,
183 PurpleSoundEventID event
)
185 PurpleConnection
*conn
= purple_account_get_connection(account
);
186 PurpleConversation
*conv
= NULL
;
189 conv
= PURPLE_CONVERSATION(purple_conversations_find_chat(conn
,id
));
191 play_conv_event(conv
, event
);
195 chat_msg_received_cb(PurpleAccount
*account
, char *sender
,
196 char *message
, PurpleChatConversation
*chat
,
197 PurpleMessageFlags flags
, PurpleSoundEventID event
)
199 PurpleConversation
*conv
= PURPLE_CONVERSATION(chat
);
200 if (flags
& PURPLE_MESSAGE_DELAYED
|| flags
& PURPLE_MESSAGE_NOTIFY
)
203 g_return_if_fail(conv
!= NULL
);
205 if (purple_chat_conversation_is_ignored_user(chat
, sender
))
208 if (chat_nick_matches_name(chat
, sender
))
211 if (flags
& PURPLE_MESSAGE_NICK
|| purple_utf8_has_word(message
, purple_chat_conversation_get_nick(chat
)))
212 /* This isn't quite right; if you have the PURPLE_SOUND_CHAT_NICK event disabled
213 * and the PURPLE_SOUND_CHAT_SAY event enabled, you won't get a sound at all */
214 play_conv_event(conv
, PURPLE_SOUND_CHAT_NICK
);
216 play_conv_event(conv
, event
);
220 got_attention_cb(PurpleAccount
*account
, const char *who
,
221 PurpleConversation
*conv
, guint type
, PurpleSoundEventID event
)
223 play_conv_event(conv
, event
);
227 * We mute sounds for the 10 seconds after you log in so that
228 * you don't get flooded with sounds when the blist shows all
229 * your buddies logging in.
232 account_signon_cb(PurpleConnection
*gc
, gpointer data
)
234 if (mute_login_sounds_timeout
!= 0)
235 g_source_remove(mute_login_sounds_timeout
);
236 mute_login_sounds
= TRUE
;
237 mute_login_sounds_timeout
= g_timeout_add_seconds(10, unmute_login_sounds_cb
, NULL
);
241 pidgin_sound_get_event_option(PurpleSoundEventID event
)
243 if(event
>= PURPLE_NUM_SOUNDS
)
246 return sounds
[event
].pref
;
250 pidgin_sound_get_event_label(PurpleSoundEventID event
)
252 if(event
>= PURPLE_NUM_SOUNDS
)
255 return sounds
[event
].label
;
259 pidgin_sound_get_handle()
267 pidgin_sound_init(void)
269 void *gtk_sound_handle
= pidgin_sound_get_handle();
270 void *blist_handle
= purple_blist_get_handle();
271 void *conv_handle
= purple_conversations_get_handle();
273 GError
*error
= NULL
;
276 purple_signal_connect(purple_connections_get_handle(), "signed-on",
277 gtk_sound_handle
, PURPLE_CALLBACK(account_signon_cb
),
280 purple_prefs_add_none(PIDGIN_PREFS_ROOT
"/sound");
281 purple_prefs_add_none(PIDGIN_PREFS_ROOT
"/sound/enabled");
282 purple_prefs_add_none(PIDGIN_PREFS_ROOT
"/sound/file");
283 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/login", TRUE
);
284 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/login", "");
285 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/logout", TRUE
);
286 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/logout", "");
287 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/im_recv", TRUE
);
288 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/im_recv", "");
289 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/first_im_recv", FALSE
);
290 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/first_im_recv", "");
291 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/send_im", TRUE
);
292 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/send_im", "");
293 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/join_chat", FALSE
);
294 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/join_chat", "");
295 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/left_chat", FALSE
);
296 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/left_chat", "");
297 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/send_chat_msg", FALSE
);
298 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/send_chat_msg", "");
299 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/chat_msg_recv", FALSE
);
300 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/chat_msg_recv", "");
301 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/nick_said", FALSE
);
302 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/nick_said", "");
303 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/pounce_default", TRUE
);
304 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/pounce_default", "");
305 purple_prefs_add_string(PIDGIN_PREFS_ROOT
"/sound/theme", "");
306 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/sent_attention", TRUE
);
307 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/sent_attention", "");
308 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/enabled/got_attention", TRUE
);
309 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/file/got_attention", "");
310 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/conv_focus", TRUE
);
311 purple_prefs_add_bool(PIDGIN_PREFS_ROOT
"/sound/mute", FALSE
);
312 purple_prefs_add_path(PIDGIN_PREFS_ROOT
"/sound/command", "");
313 purple_prefs_add_string(PIDGIN_PREFS_ROOT
"/sound/method", "automatic");
316 purple_debug_info("sound", "Initializing sound output drivers.\n");
317 gst_registry_fork_set_enabled(FALSE
);
318 if ((gst_init_failed
= !gst_init_check(NULL
, NULL
, &error
))) {
319 purple_notify_error(NULL
, _("GStreamer Failure"),
320 _("GStreamer failed to initialize."),
321 error
? error
->message
: "", NULL
);
327 #endif /* USE_GSTREAMER */
329 purple_signal_connect(blist_handle
, "buddy-signed-on",
330 gtk_sound_handle
, PURPLE_CALLBACK(buddy_state_cb
),
331 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_ARRIVE
));
332 purple_signal_connect(blist_handle
, "buddy-signed-off",
333 gtk_sound_handle
, PURPLE_CALLBACK(buddy_state_cb
),
334 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_LEAVE
));
335 purple_signal_connect(conv_handle
, "received-im-msg",
336 gtk_sound_handle
, PURPLE_CALLBACK(im_msg_received_cb
),
337 GINT_TO_POINTER(PURPLE_SOUND_RECEIVE
));
338 purple_signal_connect(conv_handle
, "sent-im-msg",
339 gtk_sound_handle
, PURPLE_CALLBACK(im_msg_sent_cb
),
340 GINT_TO_POINTER(PURPLE_SOUND_SEND
));
341 purple_signal_connect(conv_handle
, "chat-user-joined",
342 gtk_sound_handle
, PURPLE_CALLBACK(chat_user_join_cb
),
343 GINT_TO_POINTER(PURPLE_SOUND_CHAT_JOIN
));
344 purple_signal_connect(conv_handle
, "chat-user-left",
345 gtk_sound_handle
, PURPLE_CALLBACK(chat_user_left_cb
),
346 GINT_TO_POINTER(PURPLE_SOUND_CHAT_LEAVE
));
347 purple_signal_connect(conv_handle
, "sent-chat-msg",
348 gtk_sound_handle
, PURPLE_CALLBACK(chat_msg_sent_cb
),
349 GINT_TO_POINTER(PURPLE_SOUND_CHAT_YOU_SAY
));
350 purple_signal_connect(conv_handle
, "received-chat-msg",
351 gtk_sound_handle
, PURPLE_CALLBACK(chat_msg_received_cb
),
352 GINT_TO_POINTER(PURPLE_SOUND_CHAT_SAY
));
353 purple_signal_connect(conv_handle
, "got-attention", gtk_sound_handle
,
354 PURPLE_CALLBACK(got_attention_cb
),
355 GINT_TO_POINTER(PURPLE_SOUND_GOT_ATTENTION
));
356 /* for the time being, don't handle sent-attention here, since playing a
357 sound would result induplicate sounds. And fixing that would require changing the
358 conversation signal for msg-recv */
362 pidgin_sound_uninit(void)
365 if (!gst_init_failed
)
369 purple_signals_disconnect_by_handle(pidgin_sound_get_handle());
374 bus_call (GstBus
*bus
,
378 GstElement
*play
= data
;
381 switch (GST_MESSAGE_TYPE (msg
)) {
382 case GST_MESSAGE_ERROR
:
383 gst_message_parse_error(msg
, &err
, NULL
);
384 purple_debug_error("gstreamer", "%s\n", err
->message
);
386 /* fall-through and clean up */
387 case GST_MESSAGE_EOS
:
388 gst_element_set_state(play
, GST_STATE_NULL
);
389 gst_object_unref(GST_OBJECT(play
));
392 case GST_MESSAGE_WARNING
:
393 gst_message_parse_warning(msg
, &err
, NULL
);
394 purple_debug_warning("gstreamer", "%s\n", err
->message
);
406 expire_old_child(gpointer data
)
408 pid_t pid
= GPOINTER_TO_INT(data
);
410 if (waitpid(pid
, NULL
, WNOHANG
| WUNTRACED
) < 0) {
414 purple_debug_warning("gtksound", "Child is ill, pid: %d (%s)\n", pid
, strerror(errno
));
417 if (kill(pid
, SIGKILL
) < 0)
418 purple_debug_error("gtksound", "Killing process %d failed (%s)\n", pid
, strerror(errno
));
426 pidgin_sound_play_file_win32(const char *filename
)
428 wchar_t *wc_filename
= g_utf8_to_utf16(filename
,
429 -1, NULL
, NULL
, NULL
);
430 if (!PlaySoundW(wc_filename
, NULL
, SND_ASYNC
| SND_FILENAME
))
431 purple_debug(PURPLE_DEBUG_ERROR
, "sound", "Error playing sound.\n");
437 pidgin_sound_play_file(const char *filename
)
442 GstElement
*sink
= NULL
;
443 GstElement
*play
= NULL
;
447 if (purple_prefs_get_bool(PIDGIN_PREFS_ROOT
"/sound/mute"))
450 method
= purple_prefs_get_string(PIDGIN_PREFS_ROOT
"/sound/method");
452 if (purple_strequal(method
, "none")) {
454 } else if (purple_strequal(method
, "beep")) {
459 else if (purple_strequal(method
, "playsoundw")) {
460 pidgin_sound_play_file_win32(filename
);
465 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
466 purple_debug_error("gtksound", "sound file (%s) does not exist.\n", filename
);
471 if (purple_strequal(method
, "custom")) {
472 const char *sound_cmd
;
476 GError
*error
= NULL
;
479 sound_cmd
= purple_prefs_get_path(PIDGIN_PREFS_ROOT
"/sound/command");
481 if (!sound_cmd
|| *sound_cmd
== '\0') {
482 purple_debug_error("gtksound",
483 "'Command' sound method has been chosen, "
484 "but no command has been set.\n");
488 esc_filename
= g_shell_quote(filename
);
490 if(strstr(sound_cmd
, "%s"))
491 command
= purple_strreplace(sound_cmd
, "%s", esc_filename
);
493 command
= g_strdup_printf("%s %s", sound_cmd
, esc_filename
);
495 if (!g_shell_parse_argv(command
, NULL
, &argv
, &error
)) {
496 purple_debug_error("gtksound", "error parsing command %s (%s)\n",
497 command
, error
->message
);
499 g_free(esc_filename
);
504 if (!g_spawn_async(NULL
, argv
, NULL
, G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
,
505 NULL
, NULL
, &pid
, &error
)) {
506 purple_debug_error("gtksound", "sound command could not be launched: %s\n",
510 g_timeout_add_seconds(15, expire_old_child
, GINT_TO_POINTER(pid
));
514 g_free(esc_filename
);
521 if (gst_init_failed
) /* Perhaps do gdk_beep instead? */
524 if (purple_strequal(method
, "automatic")) {
525 sink
= gst_element_factory_make("directsoundsink", "sink");
527 sink
= gst_element_factory_make("waveformsink", "sink");
529 sink
= gst_element_factory_make("gconfaudiosink", "sink");
530 } else if (purple_strequal(method
, "directsound")) {
531 sink
= gst_element_factory_make("directsoundsink", "sink");
532 } else if (purple_strequal(method
, "waveform")) {
533 sink
= gst_element_factory_make("waveformsink", "sink");
536 if (purple_strequal(method
, "automatic")) {
537 sink
= gst_element_factory_make("gconfaudiosink", "sink");
538 } else if (purple_strequal(method
, "esd")) {
539 sink
= gst_element_factory_make("esdsink", "sink");
540 } else if (purple_strequal(method
, "alsa")) {
541 sink
= gst_element_factory_make("alsasink", "sink");
545 purple_debug_error("sound", "Unknown sound method '%s'\n", method
);
549 if (!purple_strequal(method
, "automatic") && !sink
) {
550 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
554 play
= gst_element_factory_make("playbin", "play");
561 uri
= g_strdup_printf("file:///%s", filename
);
562 g_strdelimit(uri
, "\\", '/');
564 uri
= g_strdup_printf("file://%s", filename
);
567 g_object_set(G_OBJECT(play
), "uri", uri
,
568 "audio-sink", sink
, NULL
);
570 bus
= gst_pipeline_get_bus(GST_PIPELINE(play
));
571 gst_bus_add_watch(bus
, bus_call
, play
);
573 gst_element_set_state(play
, GST_STATE_PLAYING
);
575 gst_object_unref(bus
);
578 #else /* #ifdef USE_GSTREAMER */
583 pidgin_sound_play_file_win32(filename
);
586 #endif /* USE_GSTREAMER */
590 pidgin_sound_play_event(PurpleSoundEventID event
)
594 const char *theme_name
;
595 PurpleSoundTheme
*theme
;
597 if ((event
== PURPLE_SOUND_BUDDY_ARRIVE
) && mute_login_sounds
)
600 if (event
>= PURPLE_NUM_SOUNDS
) {
601 purple_debug_error("sound", "got request for unknown sound: %d\n", event
);
605 enable_pref
= g_strdup_printf(PIDGIN_PREFS_ROOT
"/sound/enabled/%s",
607 file_pref
= g_strdup_printf(PIDGIN_PREFS_ROOT
"/sound/file/%s", sounds
[event
].pref
);
609 /* check NULL for sounds that don't have an option, ie buddy pounce */
610 if (purple_prefs_get_bool(enable_pref
)) {
611 char *filename
= g_strdup(purple_prefs_get_path(file_pref
));
612 theme_name
= purple_prefs_get_string(PIDGIN_PREFS_ROOT
"/sound/theme");
614 if (theme_name
&& *theme_name
&& (!filename
|| !*filename
)) {
618 theme
= PURPLE_SOUND_THEME(purple_theme_manager_find_theme(theme_name
, "sound"));
619 filename
= purple_sound_theme_get_file_full(theme
, sounds
[event
].pref
);
621 if(!g_file_test(filename
, G_FILE_TEST_IS_REGULAR
)){ /* Use Default sound in this case */
622 purple_debug_error("sound", "The file: (%s) %s\n from theme: %s, was not found or wasn't readable\n",
623 sounds
[event
].pref
, filename
, theme_name
);
629 if (!filename
|| *filename
== '\0') { /* Use Default sounds */
632 filename
= g_build_filename(PURPLE_DATADIR
,
633 "sounds", "purple", sounds
[event
].def
, NULL
);
636 purple_sound_play_file(filename
, NULL
);
646 pidgin_sound_is_customized(void)
652 for (i
= 0; i
< PURPLE_NUM_SOUNDS
; i
++) {
653 path
= g_strdup_printf(PIDGIN_PREFS_ROOT
"/sound/file/%s", sounds
[i
].pref
);
654 file
= purple_prefs_get_path(path
);
657 if (file
&& file
[0] != '\0')
665 static PurpleSoundUiOps sound_ui_ops
=
669 pidgin_sound_play_file
,
670 pidgin_sound_play_event
,
678 pidgin_sound_get_ui_ops(void)
680 return &sound_ui_ops
;