Mercurial > pidgin.yaz
annotate src/sound.c @ 733:8f3bcde94e2c
[gaim-migrate @ 743]
I like stuff.
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Mon, 21 Aug 2000 20:32:50 +0000 |
parents | c9b66c710283 |
children | 9614e69f68ca |
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) { | |
245 #ifdef ESD_SOUND | |
246 /* ESD is our player of choice. Are we OK to | |
247 * go there? */ | |
248 if (can_play_esd()) { | |
249 if (play_esd(data, size)) | |
250 _exit(0); | |
251 } | |
252 #endif | |
253 | |
254 #ifdef NAS_SOUND | |
255 /* NAS is our second choice setup. */ | |
256 if (can_play_nas()) { | |
257 if (play_nas(data, size)) | |
258 _exit(0); | |
259 } | |
260 #endif | |
261 | |
262 /* Lastly, we can try just plain old /dev/audio */ | |
263 if (can_play_audio()) { | |
264 play_audio(data, size); | |
265 _exit(0); | |
266 } | |
668
c9b66c710283
[gaim-migrate @ 678]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
655
diff
changeset
|
267 |
c9b66c710283
[gaim-migrate @ 678]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
655
diff
changeset
|
268 _exit(0); |
1 | 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 */ |