comparison finch/gntsound.c @ 19100:68a3b83795c6

Added sound to Finch. Currently it's directly into Finch, but later we'll most likely move it into a plugin since some have argued well that sound in a console app might not be an all around favored thing.
author Eric Polino <aluink@pidgin.im>
date Thu, 14 Jun 2007 03:54:49 +0000
parents
children 4c26b23e7859
comparison
equal deleted inserted replaced
19099:a1ac8b05ecdb 19100:68a3b83795c6
1 /**
2 * @file gntsound.c GNT Sound API
3 * @ingroup finch
4 *
5 * finch
6 *
7 * Finch 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 #include "internal.h"
26 #include "finch.h"
27
28 #ifdef _WIN32
29 #include <windows.h>
30 #include <mmsystem.h>
31 #endif
32
33 #ifdef USE_GSTREAMER
34 #include <gst/gst.h>
35 #endif /* USE_GSTREAMER */
36
37 #include "debug.h"
38 #include "notify.h"
39 #include "prefs.h"
40 #include "sound.h"
41 #include "util.h"
42
43 #include "gntconv.h"
44 #include "gntsound.h"
45
46 struct finch_sound_event {
47 char *label;
48 char *pref;
49 char *def;
50 };
51
52 #define PLAY_SOUND_TIMEOUT 15000
53
54 static guint mute_login_sounds_timeout = 0;
55 static gboolean mute_login_sounds = FALSE;
56
57 #ifdef USE_GSTREAMER
58 static gboolean gst_init_failed;
59 #endif /* USE_GSTREAMER */
60
61 static struct finch_sound_event sounds[PURPLE_NUM_SOUNDS] = {
62 {N_("Buddy logs in"), "login", "login.wav"},
63 {N_("Buddy logs out"), "logout", "logout.wav"},
64 {N_("Message received"), "im_recv", "receive.wav"},
65 {N_("Message received begins conversation"), "first_im_recv", "receive.wav"},
66 {N_("Message sent"), "send_im", "send.wav"},
67 {N_("Person enters chat"), "join_chat", "login.wav"},
68 {N_("Person leaves chat"), "left_chat", "logout.wav"},
69 {N_("You talk in chat"), "send_chat_msg", "send.wav"},
70 {N_("Others talk in chat"), "chat_msg_recv", "receive.wav"},
71 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
72 {NULL, "pounce_default", "alert.wav"},
73 {N_("Someone says your screen name in chat"), "nick_said", "alert.wav"}
74 };
75
76 static gboolean
77 unmute_login_sounds_cb(gpointer data)
78 {
79 mute_login_sounds = FALSE;
80 mute_login_sounds_timeout = 0;
81 return FALSE;
82 }
83
84 static gboolean
85 chat_nick_matches_name(PurpleConversation *conv, const char *aname)
86 {
87 PurpleConvChat *chat = NULL;
88 char *nick = NULL;
89 char *name = NULL;
90 gboolean ret = FALSE;
91 chat = purple_conversation_get_chat_data(conv);
92
93 if (chat==NULL)
94 return ret;
95
96 nick = g_strdup(purple_normalize(conv->account, chat->nick));
97 name = g_strdup(purple_normalize(conv->account, aname));
98
99 if (g_utf8_collate(nick, name) == 0)
100 ret = TRUE;
101
102 g_free(nick);
103 g_free(name);
104
105 return ret;
106 }
107
108 /*
109 * play a sound event for a conversation, honoring make_sound flag
110 * of conversation and checking for focus if conv_focus pref is set
111 */
112 static void
113 play_conv_event(PurpleConversation *conv, PurpleSoundEventID event)
114 {
115 /* If we should not play the sound for some reason, then exit early */
116 if (conv != NULL)
117 {
118 FinchConv *gntconv;
119 gboolean has_focus;
120
121 gntconv = FINCH_CONV(conv);
122
123 has_focus = purple_conversation_has_focus(conv);
124
125 if (has_focus && !purple_prefs_get_bool(FINCH_PREFS_ROOT "/sound/conv_focus"))
126 {
127 return;
128 }
129 }
130
131 purple_sound_play_event(event, conv ? purple_conversation_get_account(conv) : NULL);
132 }
133
134 static void
135 buddy_state_cb(PurpleBuddy *buddy, PurpleSoundEventID event)
136 {
137 purple_sound_play_event(event, purple_buddy_get_account(buddy));
138 }
139
140 static void
141 im_msg_received_cb(PurpleAccount *account, char *sender,
142 char *message, PurpleConversation *conv,
143 PurpleMessageFlags flags, PurpleSoundEventID event)
144 {
145 if (flags & PURPLE_MESSAGE_DELAYED)
146 return;
147
148 if (conv==NULL){
149 purple_sound_play_event(PURPLE_SOUND_FIRST_RECEIVE, account);
150 }
151 else{
152 play_conv_event(conv, event);
153 }
154 }
155
156 static void
157 im_msg_sent_cb(PurpleAccount *account, const char *receiver,
158 const char *message, PurpleSoundEventID event)
159 {
160 PurpleConversation *conv = purple_find_conversation_with_account(
161 PURPLE_CONV_TYPE_ANY, receiver, account);
162 play_conv_event(conv, event);
163 }
164
165 static void
166 chat_buddy_join_cb(PurpleConversation *conv, const char *name,
167 PurpleConvChatBuddyFlags flags, gboolean new_arrival,
168 PurpleSoundEventID event)
169 {
170 if (new_arrival && !chat_nick_matches_name(conv, name))
171 play_conv_event(conv, event);
172 }
173
174 static void
175 chat_buddy_left_cb(PurpleConversation *conv, const char *name,
176 const char *reason, PurpleSoundEventID event)
177 {
178 if (!chat_nick_matches_name(conv, name))
179 play_conv_event(conv, event);
180 }
181
182 static void
183 chat_msg_sent_cb(PurpleAccount *account, const char *message,
184 int id, PurpleSoundEventID event)
185 {
186 PurpleConnection *conn = purple_account_get_connection(account);
187 PurpleConversation *conv = NULL;
188
189 if (conn!=NULL)
190 conv = purple_find_chat(conn,id);
191
192 play_conv_event(conv, event);
193 }
194
195 static void
196 chat_msg_received_cb(PurpleAccount *account, char *sender,
197 char *message, PurpleConversation *conv,
198 PurpleMessageFlags flags, PurpleSoundEventID event)
199 {
200 PurpleConvChat *chat;
201
202 if (flags & PURPLE_MESSAGE_DELAYED)
203 return;
204
205 chat = purple_conversation_get_chat_data(conv);
206 g_return_if_fail(chat != NULL);
207
208 if (purple_conv_chat_is_user_ignored(chat, sender))
209 return;
210
211 if (chat_nick_matches_name(conv, sender))
212 return;
213
214 if (flags & PURPLE_MESSAGE_NICK || purple_utf8_has_word(message, chat->nick))
215 play_conv_event(conv, PURPLE_SOUND_CHAT_NICK);
216 else
217 play_conv_event(conv, event);
218 }
219
220 /*
221 * We mute sounds for the 10 seconds after you log in so that
222 * you don't get flooded with sounds when the blist shows all
223 * your buddies logging in.
224 */
225 static void
226 account_signon_cb(PurpleConnection *gc, gpointer data)
227 {
228 if (mute_login_sounds_timeout != 0)
229 g_source_remove(mute_login_sounds_timeout);
230 mute_login_sounds = TRUE;
231 mute_login_sounds_timeout = purple_timeout_add(10000, unmute_login_sounds_cb, NULL);
232 }
233
234 const char *
235 finch_sound_get_event_option(PurpleSoundEventID event)
236 {
237 if(event >= PURPLE_NUM_SOUNDS)
238 return 0;
239
240 return sounds[event].pref;
241 }
242
243 const char *
244 finch_sound_get_event_label(PurpleSoundEventID event)
245 {
246 if(event >= PURPLE_NUM_SOUNDS)
247 return NULL;
248
249 return sounds[event].label;
250 }
251
252 void *
253 finch_sound_get_handle()
254 {
255 static int handle;
256
257 return &handle;
258 }
259
260 static void
261 finch_sound_init(void)
262 {
263 void *gnt_sound_handle = finch_sound_get_handle();
264 void *blist_handle = purple_blist_get_handle();
265 void *conv_handle = purple_conversations_get_handle();
266 #ifdef USE_GSTREAMER
267 GError *error = NULL;
268 #endif
269
270 purple_signal_connect(purple_connections_get_handle(), "signed-on",
271 gnt_sound_handle, PURPLE_CALLBACK(account_signon_cb),
272 NULL);
273
274 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound");
275 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound/enabled");
276 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound/file");
277 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/login", TRUE);
278 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/login", "");
279 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/logout", TRUE);
280 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/logout", "");
281 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/im_recv", TRUE);
282 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/im_recv", "");
283 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/first_im_recv", FALSE);
284 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/first_im_recv", "");
285 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/send_im", TRUE);
286 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/send_im", "");
287 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/join_chat", FALSE);
288 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/join_chat", "");
289 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/left_chat", FALSE);
290 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/left_chat", "");
291 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/send_chat_msg", FALSE);
292 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/send_chat_msg", "");
293 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/chat_msg_recv", FALSE);
294 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/chat_msg_recv", "");
295 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/nick_said", FALSE);
296 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/nick_said", "");
297 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/enabled/pounce_default", TRUE);
298 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/file/pounce_default", "");
299 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/conv_focus", TRUE);
300 purple_prefs_add_bool(FINCH_PREFS_ROOT "/sound/mute", FALSE);
301 purple_prefs_add_path(FINCH_PREFS_ROOT "/sound/command", "");
302 purple_prefs_add_string(FINCH_PREFS_ROOT "/sound/method", "automatic");
303 purple_prefs_add_int(FINCH_PREFS_ROOT "/sound/volume", 50);
304
305 #ifdef USE_GSTREAMER
306 purple_debug_info("sound", "Initializing sound output drivers.\n");
307 if ((gst_init_failed = !gst_init_check(NULL, NULL, &error))) {
308 purple_notify_error(NULL, _("GStreamer Failure"),
309 _("GStreamer failed to initialize."),
310 error ? error->message : "");
311 if (error) {
312 g_error_free(error);
313 error = NULL;
314 }
315 }
316 #endif /* USE_GSTREAMER */
317
318 purple_signal_connect(blist_handle, "buddy-signed-on",
319 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
320 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_ARRIVE));
321 purple_signal_connect(blist_handle, "buddy-signed-off",
322 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
323 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_LEAVE));
324 purple_signal_connect(conv_handle, "received-im-msg",
325 gnt_sound_handle, PURPLE_CALLBACK(im_msg_received_cb),
326 GINT_TO_POINTER(PURPLE_SOUND_RECEIVE));
327 purple_signal_connect(conv_handle, "sent-im-msg",
328 gnt_sound_handle, PURPLE_CALLBACK(im_msg_sent_cb),
329 GINT_TO_POINTER(PURPLE_SOUND_SEND));
330 purple_signal_connect(conv_handle, "chat-buddy-joined",
331 gnt_sound_handle, PURPLE_CALLBACK(chat_buddy_join_cb),
332 GINT_TO_POINTER(PURPLE_SOUND_CHAT_JOIN));
333 purple_signal_connect(conv_handle, "chat-buddy-left",
334 gnt_sound_handle, PURPLE_CALLBACK(chat_buddy_left_cb),
335 GINT_TO_POINTER(PURPLE_SOUND_CHAT_LEAVE));
336 purple_signal_connect(conv_handle, "sent-chat-msg",
337 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_sent_cb),
338 GINT_TO_POINTER(PURPLE_SOUND_CHAT_YOU_SAY));
339 purple_signal_connect(conv_handle, "received-chat-msg",
340 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_received_cb),
341 GINT_TO_POINTER(PURPLE_SOUND_CHAT_SAY));
342 }
343
344 static void
345 finch_sound_uninit(void)
346 {
347 #ifdef USE_GSTREAMER
348 if (!gst_init_failed)
349 gst_deinit();
350 #endif
351
352 purple_signals_disconnect_by_handle(finch_sound_get_handle());
353 }
354
355 #ifdef USE_GSTREAMER
356 static gboolean
357 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
358 {
359 GstElement *play = data;
360 GError *err = NULL;
361
362 switch (GST_MESSAGE_TYPE (msg)) {
363 case GST_MESSAGE_EOS:
364 gst_element_set_state(play, GST_STATE_NULL);
365 gst_object_unref(GST_OBJECT(play));
366 break;
367 case GST_MESSAGE_ERROR:
368 gst_message_parse_error(msg, &err, NULL);
369 purple_debug_error("gstreamer", err->message);
370 g_error_free(err);
371 break;
372 case GST_MESSAGE_WARNING:
373 gst_message_parse_warning(msg, &err, NULL);
374 purple_debug_warning("gstreamer", err->message);
375 g_error_free(err);
376 break;
377 default:
378 break;
379 }
380 return TRUE;
381 }
382 #endif
383
384 static void
385 finch_sound_play_file(const char *filename)
386 {
387 const char *method;
388 #ifdef USE_GSTREAMER
389 float volume;
390 char *uri;
391 GstElement *sink = NULL;
392 GstElement *play = NULL;
393 GstBus *bus = NULL;
394 #endif
395 if (purple_prefs_get_bool(FINCH_PREFS_ROOT "/sound/mute"))
396 return;
397
398 method = purple_prefs_get_string(FINCH_PREFS_ROOT "/sound/method");
399
400 if (!strcmp(method, "none")) {
401 return;
402 } else if (!strcmp(method, "beep")) {
403 beep();
404 return;
405 }
406
407 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
408 purple_debug_error("gntsound", "sound file (%s) does not exist.\n", filename);
409 return;
410 }
411
412 #ifndef _WIN32
413 if (!strcmp(method, "custom")) {
414 const char *sound_cmd;
415 char *command;
416 char *esc_filename;
417 GError *error = NULL;
418
419 sound_cmd = purple_prefs_get_path(FINCH_PREFS_ROOT "/sound/command");
420
421 if (!sound_cmd || *sound_cmd == '\0') {
422 purple_debug_error("gntsound",
423 "'Command' sound method has been chosen, "
424 "but no command has been set.");
425 return;
426 }
427
428 esc_filename = g_shell_quote(filename);
429
430 if(strstr(sound_cmd, "%s"))
431 command = purple_strreplace(sound_cmd, "%s", esc_filename);
432 else
433 command = g_strdup_printf("%s %s", sound_cmd, esc_filename);
434
435 if(!g_spawn_command_line_async(command, &error)) {
436 purple_debug_error("gntsound", "sound command could not be launched: %s\n", error->message);
437 g_error_free(error);
438 }
439
440 g_free(esc_filename);
441 g_free(command);
442 return;
443 }
444 #ifdef USE_GSTREAMER
445 if (gst_init_failed) /* Perhaps do beep instead? */
446 return;
447 volume = (float)(CLAMP(purple_prefs_get_int(FINCH_PREFS_ROOT "/sound/volume"),0,100)) / 50;
448 if (!strcmp(method, "automatic")) {
449 if (purple_running_gnome()) {
450 sink = gst_element_factory_make("gconfaudiosink", "sink");
451 }
452 if (!sink)
453 sink = gst_element_factory_make("autoaudiosink", "sink");
454 if (!sink) {
455 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
456 return;
457 }
458 } else if (!strcmp(method, "esd")) {
459 sink = gst_element_factory_make("esdsink", "sink");
460 if (!sink) {
461 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
462 return;
463 }
464 } else if (!strcmp(method, "alsa")) {
465 sink = gst_element_factory_make("alsasink", "sink");
466 if (!sink) {
467 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
468 return;
469 }
470 } else {
471 purple_debug_error("sound", "Unknown sound method '%s'\n", method);
472 return;
473 }
474
475 play = gst_element_factory_make("playbin", "play");
476
477 if (play == NULL) {
478 return;
479 }
480
481 uri = g_strdup_printf("file://%s", filename);
482
483 g_object_set(G_OBJECT(play), "uri", uri,
484 "volume", volume,
485 "audio-sink", sink, NULL);
486
487 bus = gst_pipeline_get_bus(GST_PIPELINE(play));
488 gst_bus_add_watch(bus, bus_call, play);
489
490 gst_element_set_state(play, GST_STATE_PLAYING);
491
492 gst_object_unref(bus);
493 g_free(uri);
494
495 #else /* USE_GSTREAMER */
496 beep();
497 return;
498 #endif /* USE_GSTREAMER */
499 #else /* _WIN32 */
500 purple_debug_info("sound", "Playing %s\n", filename);
501
502 if (G_WIN32_HAVE_WIDECHAR_API ()) {
503 wchar_t *wc_filename = g_utf8_to_utf16(filename,
504 -1, NULL, NULL, NULL);
505 if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME))
506 purple_debug(PURPLE_DEBUG_ERROR, "sound", "Error playing sound.\n");
507 g_free(wc_filename);
508 } else {
509 char *l_filename = g_locale_from_utf8(filename,
510 -1, NULL, NULL, NULL);
511 if (!PlaySoundA(l_filename, NULL, SND_ASYNC | SND_FILENAME))
512 purple_debug(PURPLE_DEBUG_ERROR, "sound", "Error playing sound.\n");
513 g_free(l_filename);
514 }
515 #endif /* _WIN32 */
516 }
517
518 static void
519 finch_sound_play_event(PurpleSoundEventID event)
520 {
521 char *enable_pref;
522 char *file_pref;
523 if ((event == PURPLE_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
524 return;
525
526 if (event >= PURPLE_NUM_SOUNDS) {
527 purple_debug_error("sound", "got request for unknown sound: %d\n", event);
528 return;
529 }
530
531 enable_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/enabled/%s",
532 sounds[event].pref);
533 file_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/file/%s", sounds[event].pref);
534
535 /* check NULL for sounds that don't have an option, ie buddy pounce */
536 if (purple_prefs_get_bool(enable_pref)) {
537 char *filename = g_strdup(purple_prefs_get_path(file_pref));
538 if(!filename || !strlen(filename)) {
539 g_free(filename);
540 filename = g_build_filename(DATADIR, "sounds", "finch", sounds[event].def, NULL);
541 }
542
543 purple_sound_play_file(filename, NULL);
544 g_free(filename);
545 }
546
547 g_free(enable_pref);
548 g_free(file_pref);
549 }
550
551 static PurpleSoundUiOps sound_ui_ops =
552 {
553 finch_sound_init,
554 finch_sound_uninit,
555 finch_sound_play_file,
556 finch_sound_play_event,
557 NULL,
558 NULL,
559 NULL,
560 NULL
561 };
562
563 PurpleSoundUiOps *
564 finch_sound_get_ui_ops(void)
565 {
566 return &sound_ui_ops;
567 }