Mercurial > pidgin
annotate src/sound.c @ 900:e18815f5a4e9
[gaim-migrate @ 910]
libfaim changes la la la
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Wed, 13 Sep 2000 23:48:43 +0000 |
parents | 88d1edcd7cd6 |
children | 0a4d0ed65e17 |
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 |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
23 #include "../config.h" |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
24 #endif |
1 | 25 #include <stdio.h> |
26 #include <string.h> | |
27 #include <sys/time.h> | |
28 #include <unistd.h> | |
29 #include <gtk/gtk.h> | |
30 #include <fcntl.h> | |
31 #include <sys/wait.h> | |
32 #include <sys/signal.h> | |
63 | 33 #include <unistd.h> |
34 #include <sys/types.h> | |
35 #include <sys/stat.h> | |
1 | 36 |
37 #ifdef ESD_SOUND | |
38 #include <esd.h> | |
39 #endif | |
40 | |
41 #ifdef NAS_SOUND | |
42 #include <audio/audiolib.h> | |
43 #endif | |
44 | |
45 #include "gaim.h" | |
46 #include "sounds/BuddyArrive.h" | |
47 #include "sounds/BuddyLeave.h" | |
48 #include "sounds/Send.h" | |
49 #include "sounds/Receive.h" | |
50 | |
655
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
51 static int check_dev(char *dev) |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
52 { |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
53 struct stat stat_buf; |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
54 uid_t user = getuid(); |
899
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
55 gid_t group = getgid(), |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
56 other_groups[32]; |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
57 int i, numgroups; |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
58 |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
59 if ((numgroups = getgroups (32, other_groups)) == -1) |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
60 return 0; |
655
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
61 if (stat(dev, &stat_buf)) |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
62 return 0; |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
63 if (user == stat_buf.st_uid && stat_buf.st_mode & S_IWUSR) |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
64 return 1; |
899
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
65 if (stat_buf.st_mode & S_IWGRP) { |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
66 if (group == stat_buf.st_gid) |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
67 return 1; |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
68 for (i = 0; i < numgroups; i++) |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
69 if (other_groups[i] == stat_buf.st_gid) |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
70 return 1; |
88d1edcd7cd6
[gaim-migrate @ 909]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
778
diff
changeset
|
71 } |
655
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
72 if (stat_buf.st_mode & S_IWOTH) |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
73 return 1; |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
74 return 0; |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
75 } |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
76 |
1 | 77 |
78 static void play_audio(char *data, int size) | |
79 { | |
80 int fd; | |
81 | |
82 fd = open("/dev/audio", O_WRONLY | O_EXCL); | |
83 if (fd < 0) | |
84 return; | |
85 write(fd, data, size); | |
86 close(fd); | |
87 } | |
88 | |
89 static int can_play_audio() | |
90 { | |
655
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
91 return check_dev("/dev/audio"); |
1 | 92 } |
93 | |
94 | |
95 #ifdef ESD_SOUND | |
96 /* | |
97 ** This routine converts from ulaw to 16 bit linear. | |
98 ** | |
99 ** Craig Reese: IDA/Supercomputing Research Center | |
100 ** 29 September 1989 | |
101 ** | |
102 ** References: | |
103 ** 1) CCITT Recommendation G.711 (very difficult to follow) | |
104 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards | |
105 ** for Analog-to_Digital Conversion Techniques," | |
106 ** 17 February 1987 | |
107 ** | |
108 ** Input: 8 bit ulaw sample | |
109 ** Output: signed 16 bit linear sample | |
110 ** Z-note -- this is from libaudiofile. Thanks guys! | |
111 */ | |
112 | |
113 int _af_ulaw2linear (unsigned char ulawbyte) | |
114 { | |
115 static int exp_lut[8] = {0,132,396,924,1980,4092,8316,16764}; | |
116 int sign, exponent, mantissa, sample; | |
117 | |
118 ulawbyte = ~ulawbyte; | |
119 sign = (ulawbyte & 0x80); | |
120 exponent = (ulawbyte >> 4) & 0x07; | |
121 mantissa = ulawbyte & 0x0F; | |
122 sample = exp_lut[exponent] + (mantissa << (exponent + 3)); | |
123 if (sign != 0) sample = -sample; | |
124 | |
125 return(sample); | |
126 } | |
127 | |
128 | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
129 int esd_fd; |
1 | 130 |
131 static int play_esd(unsigned char *data, int size) | |
132 { | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
133 int i; |
79 | 134 guint16 *lineardata; |
1 | 135 |
79 | 136 lineardata = g_malloc(size * 2); |
1 | 137 |
138 for (i=0; i<size; i++) | |
139 lineardata[i] = _af_ulaw2linear(data[i]); | |
140 | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
141 write(esd_fd, lineardata, size * 2); |
1 | 142 |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
143 close(esd_fd); |
1 | 144 g_free(lineardata); |
145 | |
146 return 1; | |
147 | |
148 } | |
149 | |
150 static int can_play_esd() | |
151 { | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
152 esd_format_t format = ESD_BITS16 | ESD_STREAM | ESD_PLAY | ESD_MONO; |
655
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
153 |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
154 esd_fd = esd_play_stream(format, 8012, NULL, "gaim"); |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
155 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
156 if (esd_fd < 0) { |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
157 return 0; |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
158 } |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
159 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
160 return 1; |
1 | 161 } |
162 | |
163 #endif | |
164 | |
165 #ifdef NAS_SOUND | |
166 | |
167 char nas_server[] = "localhost"; | |
168 AuServer *nas_serv = NULL; | |
169 | |
170 static AuBool | |
171 NasEventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler) | |
172 { | |
173 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev; | |
174 | |
175 if (ev->type == AuEventTypeElementNotify) { | |
176 switch (event->kind) { | |
177 case AuElementNotifyKindState: | |
178 switch (event->cur_state) { | |
179 case AuStateStop: | |
180 _exit(0); | |
181 } | |
182 break; | |
183 } | |
184 } | |
185 return AuTrue; | |
186 } | |
187 | |
188 | |
189 static int play_nas(unsigned char *data, int size) | |
190 { | |
191 AuDeviceID device = AuNone; | |
192 AuFlowID flow; | |
193 AuElement elements[3]; | |
194 int i, n, w; | |
195 | |
196 /* look for an output device */ | |
197 for (i = 0; i < AuServerNumDevices(nas_serv); i++) { | |
198 if ((AuDeviceKind(AuServerDevice(nas_serv, i)) == | |
199 AuComponentKindPhysicalOutput) && | |
200 AuDeviceNumTracks(AuServerDevice(nas_serv, i)) == 1) { | |
201 device = AuDeviceIdentifier(AuServerDevice(nas_serv, i)); | |
202 break; | |
203 } | |
204 } | |
205 | |
206 if (device == AuNone) | |
207 return 0; | |
208 | |
209 if (!(flow = AuCreateFlow(nas_serv, NULL))) | |
210 return 0; | |
211 | |
212 | |
213 AuMakeElementImportClient(&elements[0], 8012, AuFormatULAW8, | |
214 1, AuTrue, size, size/2, 0, NULL); | |
215 AuMakeElementExportDevice(&elements[1], 0, device, 8012, | |
216 AuUnlimitedSamples, 0, NULL); | |
217 AuSetElements(nas_serv, flow, AuTrue, 2, elements, NULL); | |
218 | |
219 AuStartFlow(nas_serv, flow, NULL); | |
220 | |
221 AuWriteElement(nas_serv, flow, 0, size, data, AuTrue, NULL); | |
222 | |
223 AuRegisterEventHandler(nas_serv, AuEventHandlerIDMask, 0, flow, | |
224 NasEventHandler, NULL); | |
225 | |
226 while(1) { | |
227 AuHandleEvents(nas_serv); | |
228 } | |
229 | |
230 return 1; | |
231 } | |
232 | |
233 static int can_play_nas() | |
234 { | |
235 if ((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) | |
236 return 1; | |
237 return 0; | |
238 } | |
239 | |
240 #endif | |
241 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
242 void play(unsigned char *data, int size) |
1 | 243 { |
244 int pid; | |
245 | |
246 #ifdef _WIN32 | |
247 return; | |
248 #endif | |
249 | |
250 pid = fork(); | |
251 | |
252 if (pid < 0) | |
253 return; | |
254 else if (pid == 0) { | |
775
9614e69f68ca
[gaim-migrate @ 785]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
668
diff
changeset
|
255 if (sound_options & OPT_SOUND_BEEP) { |
777
1d60fb2c3b3b
[gaim-migrate @ 787]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
775
diff
changeset
|
256 printf("\a"); |
778
035d3115c6fe
[gaim-migrate @ 788]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
777
diff
changeset
|
257 fflush(stdout); |
775
9614e69f68ca
[gaim-migrate @ 785]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
668
diff
changeset
|
258 _exit(0); |
9614e69f68ca
[gaim-migrate @ 785]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
668
diff
changeset
|
259 } |
9614e69f68ca
[gaim-migrate @ 785]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
668
diff
changeset
|
260 |
1 | 261 #ifdef ESD_SOUND |
262 /* ESD is our player of choice. Are we OK to | |
263 * go there? */ | |
264 if (can_play_esd()) { | |
265 if (play_esd(data, size)) | |
266 _exit(0); | |
267 } | |
268 #endif | |
269 | |
270 #ifdef NAS_SOUND | |
271 /* NAS is our second choice setup. */ | |
272 if (can_play_nas()) { | |
273 if (play_nas(data, size)) | |
274 _exit(0); | |
275 } | |
276 #endif | |
277 | |
278 /* Lastly, we can try just plain old /dev/audio */ | |
279 if (can_play_audio()) { | |
280 play_audio(data, size); | |
281 _exit(0); | |
282 } | |
668
c9b66c710283
[gaim-migrate @ 678]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
655
diff
changeset
|
283 |
c9b66c710283
[gaim-migrate @ 678]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
655
diff
changeset
|
284 _exit(0); |
1 | 285 } else { |
286 gtk_timeout_add(100, (GtkFunction)clean_pid, NULL); | |
287 } | |
288 } | |
289 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
290 extern int logins_not_muted; |
1 | 291 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
292 #ifdef USE_GNOME |
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
293 void gnome_play_sound(int sound) |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
294 #else |
1 | 295 void play_sound(int sound) |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
296 #endif |
1 | 297 { |
298 | |
299 switch(sound) { | |
300 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
301 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
1 | 302 play(BuddyArrive, sizeof(BuddyArrive)); |
303 break; | |
304 case BUDDY_LEAVE: | |
305 if (sound_options & OPT_SOUND_LOGOUT) | |
306 play(BuddyLeave, sizeof(BuddyLeave)); | |
307 break; | |
308 case SEND: | |
309 if (sound_options & OPT_SOUND_SEND) | |
310 play(Send, sizeof(Send)); | |
311 break; | |
312 case FIRST_RECEIVE: | |
313 if (sound_options & OPT_SOUND_FIRST_RCV) | |
314 play(Receive, sizeof(Receive)); | |
315 break; | |
316 case RECEIVE: | |
317 if (sound_options & OPT_SOUND_RECV) | |
318 play(Receive, sizeof(Receive)); | |
319 break; | |
320 case AWAY: | |
321 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
322 play(Receive, sizeof(Receive)); | |
323 break; | |
324 } | |
64 | 325 } |
1 | 326 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
327 #ifdef USE_GNOME |
64 | 328 |
82 | 329 #include <gnome.h> |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
330 void play_sound(int sound) |
64 | 331 { |
332 | |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
333 if (!(sound_options & OPT_SOUND_THROUGH_GNOME)) { |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
334 gnome_play_sound(sound); |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
335 return; |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
336 } |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
337 |
64 | 338 switch(sound) { |
339 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
340 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
341 gnome_triggers_do("", "program", "gaim", "login", NULL); |
64 | 342 break; |
343 case BUDDY_LEAVE: | |
344 if (sound_options & OPT_SOUND_LOGOUT) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
345 gnome_triggers_do("", "program", "gaim", "leave", NULL); |
64 | 346 break; |
347 case SEND: | |
348 if (sound_options & OPT_SOUND_SEND) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
349 gnome_triggers_do("", "program", "gaim", "send", NULL); |
64 | 350 break; |
351 case FIRST_RECEIVE: | |
352 if (sound_options & OPT_SOUND_FIRST_RCV) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
353 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 354 break; |
355 case RECEIVE: | |
356 if (sound_options & OPT_SOUND_RECV) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
357 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 358 break; |
359 case AWAY: | |
360 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
361 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 362 break; |
363 } | |
1 | 364 } |
64 | 365 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
366 #endif /* USE_GNOME */ |