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