Mercurial > pidgin.yaz
annotate src/sound.c @ 655:6ba20a0089d8
[gaim-migrate @ 665]
added another check to can_play_esd (or whatever) to make sure /dev/dsp is available
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Thu, 10 Aug 2000 05:02:01 +0000 |
parents | eaab8abda2c0 |
children | c9b66c710283 |
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 |
6ba20a0089d8
[gaim-migrate @ 665]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
606
diff
changeset
|
144 if (!check_dev("/dev/dsp")) return 0; |
354
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 esd_fd = esd_play_stream(format, 8012, NULL, "gaim"); |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
147 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
148 if (esd_fd < 0) { |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
149 return 0; |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
150 } |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
151 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
152 return 1; |
1 | 153 } |
154 | |
155 #endif | |
156 | |
157 #ifdef NAS_SOUND | |
158 | |
159 char nas_server[] = "localhost"; | |
160 AuServer *nas_serv = NULL; | |
161 | |
162 static AuBool | |
163 NasEventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler) | |
164 { | |
165 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev; | |
166 | |
167 if (ev->type == AuEventTypeElementNotify) { | |
168 switch (event->kind) { | |
169 case AuElementNotifyKindState: | |
170 switch (event->cur_state) { | |
171 case AuStateStop: | |
172 _exit(0); | |
173 } | |
174 break; | |
175 } | |
176 } | |
177 return AuTrue; | |
178 } | |
179 | |
180 | |
181 static int play_nas(unsigned char *data, int size) | |
182 { | |
183 AuDeviceID device = AuNone; | |
184 AuFlowID flow; | |
185 AuElement elements[3]; | |
186 int i, n, w; | |
187 | |
188 /* look for an output device */ | |
189 for (i = 0; i < AuServerNumDevices(nas_serv); i++) { | |
190 if ((AuDeviceKind(AuServerDevice(nas_serv, i)) == | |
191 AuComponentKindPhysicalOutput) && | |
192 AuDeviceNumTracks(AuServerDevice(nas_serv, i)) == 1) { | |
193 device = AuDeviceIdentifier(AuServerDevice(nas_serv, i)); | |
194 break; | |
195 } | |
196 } | |
197 | |
198 if (device == AuNone) | |
199 return 0; | |
200 | |
201 if (!(flow = AuCreateFlow(nas_serv, NULL))) | |
202 return 0; | |
203 | |
204 | |
205 AuMakeElementImportClient(&elements[0], 8012, AuFormatULAW8, | |
206 1, AuTrue, size, size/2, 0, NULL); | |
207 AuMakeElementExportDevice(&elements[1], 0, device, 8012, | |
208 AuUnlimitedSamples, 0, NULL); | |
209 AuSetElements(nas_serv, flow, AuTrue, 2, elements, NULL); | |
210 | |
211 AuStartFlow(nas_serv, flow, NULL); | |
212 | |
213 AuWriteElement(nas_serv, flow, 0, size, data, AuTrue, NULL); | |
214 | |
215 AuRegisterEventHandler(nas_serv, AuEventHandlerIDMask, 0, flow, | |
216 NasEventHandler, NULL); | |
217 | |
218 while(1) { | |
219 AuHandleEvents(nas_serv); | |
220 } | |
221 | |
222 return 1; | |
223 } | |
224 | |
225 static int can_play_nas() | |
226 { | |
227 if ((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) | |
228 return 1; | |
229 return 0; | |
230 } | |
231 | |
232 #endif | |
233 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
234 void play(unsigned char *data, int size) |
1 | 235 { |
236 int pid; | |
237 | |
238 #ifdef _WIN32 | |
239 return; | |
240 #endif | |
241 | |
242 pid = fork(); | |
243 | |
244 if (pid < 0) | |
245 return; | |
246 else if (pid == 0) { | |
247 #ifdef ESD_SOUND | |
248 /* ESD is our player of choice. Are we OK to | |
249 * go there? */ | |
250 if (can_play_esd()) { | |
251 if (play_esd(data, size)) | |
252 _exit(0); | |
253 } | |
254 #endif | |
255 | |
256 #ifdef NAS_SOUND | |
257 /* NAS is our second choice setup. */ | |
258 if (can_play_nas()) { | |
259 if (play_nas(data, size)) | |
260 _exit(0); | |
261 } | |
262 #endif | |
263 | |
264 /* Lastly, we can try just plain old /dev/audio */ | |
265 if (can_play_audio()) { | |
266 play_audio(data, size); | |
267 _exit(0); | |
268 } | |
269 } else { | |
270 gtk_timeout_add(100, (GtkFunction)clean_pid, NULL); | |
271 } | |
272 } | |
273 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
274 extern int logins_not_muted; |
1 | 275 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
276 #ifdef USE_GNOME |
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
277 void gnome_play_sound(int sound) |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
278 #else |
1 | 279 void play_sound(int sound) |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
280 #endif |
1 | 281 { |
282 | |
283 switch(sound) { | |
284 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
285 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
1 | 286 play(BuddyArrive, sizeof(BuddyArrive)); |
287 break; | |
288 case BUDDY_LEAVE: | |
289 if (sound_options & OPT_SOUND_LOGOUT) | |
290 play(BuddyLeave, sizeof(BuddyLeave)); | |
291 break; | |
292 case SEND: | |
293 if (sound_options & OPT_SOUND_SEND) | |
294 play(Send, sizeof(Send)); | |
295 break; | |
296 case FIRST_RECEIVE: | |
297 if (sound_options & OPT_SOUND_FIRST_RCV) | |
298 play(Receive, sizeof(Receive)); | |
299 break; | |
300 case RECEIVE: | |
301 if (sound_options & OPT_SOUND_RECV) | |
302 play(Receive, sizeof(Receive)); | |
303 break; | |
304 case AWAY: | |
305 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
306 play(Receive, sizeof(Receive)); | |
307 break; | |
308 } | |
64 | 309 } |
1 | 310 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
311 #ifdef USE_GNOME |
64 | 312 |
82 | 313 #include <gnome.h> |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
314 void play_sound(int sound) |
64 | 315 { |
316 | |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
317 if (!(sound_options & OPT_SOUND_THROUGH_GNOME)) { |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
318 gnome_play_sound(sound); |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
319 return; |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
320 } |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
321 |
64 | 322 switch(sound) { |
323 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
324 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
325 gnome_triggers_do("", "program", "gaim", "login", NULL); |
64 | 326 break; |
327 case BUDDY_LEAVE: | |
328 if (sound_options & OPT_SOUND_LOGOUT) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
329 gnome_triggers_do("", "program", "gaim", "leave", NULL); |
64 | 330 break; |
331 case SEND: | |
332 if (sound_options & OPT_SOUND_SEND) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
333 gnome_triggers_do("", "program", "gaim", "send", NULL); |
64 | 334 break; |
335 case FIRST_RECEIVE: | |
336 if (sound_options & OPT_SOUND_FIRST_RCV) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
337 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 338 break; |
339 case RECEIVE: | |
340 if (sound_options & OPT_SOUND_RECV) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
341 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 342 break; |
343 case AWAY: | |
344 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
345 gnome_triggers_do("", "program", "gaim", "recv", NULL); |
64 | 346 break; |
347 } | |
1 | 348 } |
64 | 349 |
606
eaab8abda2c0
[gaim-migrate @ 616]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
451
diff
changeset
|
350 #endif /* USE_GNOME */ |