Mercurial > pidgin.yaz
annotate src/sound.c @ 4429:bf770f11132b
[gaim-migrate @ 4704]
(15:41:59) Robot101: the hunk that got left out in the cold.
its a good thing when things compile
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Sun, 26 Jan 2003 20:41:49 +0000 |
parents | 4b04ecb3eb97 |
children | 801830dec409 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2048
diff
changeset
|
23 #include <config.h> |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
24 #endif |
1 | 25 #include <stdio.h> |
1057
d50d3abb9eb7
[gaim-migrate @ 1067]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1006
diff
changeset
|
26 #include <stdlib.h> |
1 | 27 #include <string.h> |
3630 | 28 |
29 #ifndef _WIN32 | |
1 | 30 #include <sys/time.h> |
31 #include <unistd.h> | |
32 #include <sys/wait.h> | |
63 | 33 #include <unistd.h> |
3630 | 34 #else |
35 #include <windows.h> | |
36 #include <mmsystem.h> | |
37 #endif | |
38 | |
39 #include <fcntl.h> | |
63 | 40 #include <sys/types.h> |
41 #include <sys/stat.h> | |
1 | 42 |
4429 | 43 #ifdef USE_AO |
44 #include <ao/ao.h> | |
45 #include <audiofile.h> | |
46 #endif /* USE_AO */ | |
4012 | 47 |
1 | 48 #include "gaim.h" |
49 | |
4019
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
50 #ifdef _WIN32 |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
51 #include "win32dep.h" |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
52 #endif |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
53 |
4429 | 54 #ifdef USE_AO |
55 static gboolean ao_initialized = FALSE; | |
56 #endif /* USE_AO */ | |
57 | |
3319 | 58 gboolean mute_sounds = 0; |
59 | |
4082 | 60 /* description, option bit, default sound file * |
61 * set the option bit to 0 to have it not display in prefs * | |
62 * the order here has to match the defines in gaim.h. * | |
63 * -Robot101 */ | |
3060 | 64 struct sound_struct sounds[NUM_SOUNDS] = { |
4013 | 65 {N_("Buddy logs in"), OPT_SOUND_LOGIN, "arrive.wav"}, |
66 {N_("Buddy logs out"), OPT_SOUND_LOGOUT, "leave.wav"}, | |
4012 | 67 {N_("Message received"), OPT_SOUND_RECV, "receive.wav"}, |
68 {N_("Message received begins conversation"), OPT_SOUND_FIRST_RCV, "receive.wav"}, | |
69 {N_("Message sent"), OPT_SOUND_SEND, "send.wav"}, | |
4013 | 70 {N_("Person enters chat"), OPT_SOUND_CHAT_JOIN, "arrive.wav"}, |
71 {N_("Person leaves chat"), OPT_SOUND_CHAT_PART, "leave.wav"}, | |
4012 | 72 {N_("You talk in chat"), OPT_SOUND_CHAT_YOU_SAY, "send.wav"}, |
73 {N_("Others talk in chat"), OPT_SOUND_CHAT_SAY, "receive.wav"}, | |
3326 | 74 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ |
4012 | 75 {NULL, 0, "redalert.wav"}, |
76 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"} | |
3060 | 77 }; |
78 | |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
79 void play_file(char *filename) |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
80 { |
3630 | 81 #ifndef _WIN32 |
4429 | 82 #ifdef USE_AO |
83 int ao_driver; | |
84 #endif /* USE_AO */ | |
85 pid_t pid; | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
86 #endif |
4429 | 87 |
3901 | 88 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY)) |
89 return; /* check here in case a buddy pounce plays a file while away */ | |
4429 | 90 |
3004 | 91 if (sound_options & OPT_SOUND_BEEP) { |
92 gdk_beep(); | |
93 return; | |
94 } | |
95 | |
4429 | 96 #ifndef _WIN32 |
97 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
98 gchar *tmp = g_strdup_printf(_("Unable to play sound because the chosen filename (%s) does not exist."), filename); | |
99 do_error_dialog(tmp, NULL, GAIM_ERROR); | |
100 g_free(tmp); | |
101 return; | |
3060 | 102 } |
4429 | 103 |
104 if (sound_options & OPT_SOUND_CMD) { | |
105 gchar *command = NULL; | |
106 GError *error = NULL; | |
107 | |
108 if (!sound_cmd[0]) { | |
109 do_error_dialog(_("Unable to play sound because the 'Command' sound method has been chosen, but no command has been set."), NULL, GAIM_ERROR); | |
110 return; | |
111 } | |
112 | |
113 command = g_strdup_printf(sound_cmd, filename); | |
114 | |
115 if (g_spawn_command_line_async(command, &error) == FALSE) { | |
116 gchar *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
117 do_error_dialog(tmp, NULL, GAIM_ERROR); | |
118 g_free(tmp); | |
119 g_error_free(error); | |
120 } | |
121 | |
122 g_free(command); | |
123 return; | |
124 } | |
125 | |
126 #ifdef USE_AO | |
127 if (!ao_initialized) { | |
128 ao_initialize(); | |
129 } | |
130 | |
131 ao_driver = ao_default_driver_id(); | |
132 | |
133 if (ao_driver == -1) { | |
134 do_error_dialog(_("Unable to play sound because no suitable driver could be found."), NULL, GAIM_ERROR); | |
135 return; | |
136 } | |
137 | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
138 pid = fork(); |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
139 |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
140 if (pid < 0) |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
141 return; |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
142 else if (pid == 0) { |
4429 | 143 AFfilehandle file = afOpenFile(filename, "rb", NULL); |
144 if(file) { | |
145 ao_device *device; | |
146 ao_sample_format format; | |
2777
0899f8c99a15
[gaim-migrate @ 2790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2680
diff
changeset
|
147 |
4429 | 148 int in_fmt; |
149 int bytes_per_frame; | |
150 | |
151 format.rate = afGetRate(file, AF_DEFAULT_TRACK); | |
152 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
153 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
154 &format.bits); | |
155 | |
156 bytes_per_frame = format.bits * format.channels / 8; | |
2777
0899f8c99a15
[gaim-migrate @ 2790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2680
diff
changeset
|
157 |
4429 | 158 device = ao_open_live(ao_driver, &format, NULL); |
159 | |
160 if (device) { | |
161 int frames_read; | |
162 char buf[4096]; | |
163 int buf_frames = sizeof(buf) / bytes_per_frame; | |
164 | |
165 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
166 buf, buf_frames))) { | |
167 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
168 break; | |
169 } | |
170 ao_close(device); | |
171 } | |
172 | |
173 ao_shutdown(); | |
174 afCloseFile(file); | |
2580
86eaeb064e82
[gaim-migrate @ 2593]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2478
diff
changeset
|
175 } |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
176 _exit(0); |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
177 } |
4429 | 178 #else /* USE_AO */ |
179 gdk_beep(); | |
180 #endif /* USE_AO */ | |
181 | |
3630 | 182 #else /* _WIN32 */ |
183 debug_printf("Playing %s\n", filename); | |
184 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) | |
185 debug_printf("Error playing sound."); | |
186 #endif | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
187 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
188 |
4429 | 189 void sound_quit() { |
190 #ifdef USE_AO | |
191 if (ao_initialized) { | |
192 ao_shutdown(); | |
193 } | |
194 #endif | |
195 } | |
196 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
197 extern int logins_not_muted; |
1 | 198 |
199 void play_sound(int sound) | |
200 { | |
3319 | 201 if (mute_sounds) |
202 return; | |
4429 | 203 |
3060 | 204 if ((sound == SND_BUDDY_ARRIVE) && !logins_not_muted) |
205 return; | |
206 | |
207 if (sound >= NUM_SOUNDS) { | |
4082 | 208 debug_printf("got request for unknown sound: %d\n", sound); |
3060 | 209 return; |
210 } | |
211 | |
212 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
3072 | 213 if ((sound_options & sounds[sound].opt) || (sounds[sound].opt == 0)) { |
3060 | 214 if (sound_file[sound]) { |
215 play_file(sound_file[sound]); | |
216 } else { | |
4004 | 217 gchar *filename = NULL; |
218 | |
219 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[sound].def, NULL); | |
220 play_file(filename); | |
221 g_free(filename); | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
222 } |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
223 } |
64 | 224 } |