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