Mercurial > pidgin
annotate src/sound.c @ 4493:61ba567f9c64
[gaim-migrate @ 4768]
include win32dep.h
committer: Tailor Script <tailor@pidgin.im>
| author | Herman Bloggs <hermanator12002@yahoo.com> |
|---|---|
| date | Fri, 31 Jan 2003 19:44:20 +0000 |
| parents | 3196d9044a45 |
| children | 9df99116840a |
| 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 |
|
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2048
diff
changeset
|
23 #include <config.h> |
|
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
24 #endif |
| 1 | 25 #include <stdio.h> |
|
1057
d50d3abb9eb7
[gaim-migrate @ 1067]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1006
diff
changeset
|
26 #include <stdlib.h> |
| 1 | 27 #include <string.h> |
| 3630 | 28 |
| 29 #ifndef _WIN32 | |
| 1 | 30 #include <sys/time.h> |
| 31 #include <unistd.h> | |
| 32 #include <sys/wait.h> | |
| 63 | 33 #include <unistd.h> |
| 3630 | 34 #else |
| 35 #include <windows.h> | |
| 36 #include <mmsystem.h> | |
| 37 #endif | |
| 38 | |
| 39 #include <fcntl.h> | |
| 63 | 40 #include <sys/types.h> |
| 41 #include <sys/stat.h> | |
| 1 | 42 |
| 4430 | 43 #ifdef ESD_SOUND |
| 44 #include <esd.h> | |
| 45 #endif | |
| 46 | |
| 47 #ifdef ARTSC_SOUND | |
| 48 #include <artsc.h> | |
| 49 #endif | |
| 50 | |
| 51 #ifdef NAS_SOUND | |
| 52 #include <audio/audiolib.h> | |
| 53 #endif | |
| 4012 | 54 |
| 1 | 55 #include "gaim.h" |
| 56 | |
|
4019
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
57 #ifdef _WIN32 |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
58 #include "win32dep.h" |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
59 #endif |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
60 |
| 3319 | 61 gboolean mute_sounds = 0; |
| 62 | |
| 4082 | 63 /* description, option bit, default sound file * |
| 64 * set the option bit to 0 to have it not display in prefs * | |
| 65 * the order here has to match the defines in gaim.h. * | |
| 66 * -Robot101 */ | |
| 3060 | 67 struct sound_struct sounds[NUM_SOUNDS] = { |
| 4013 | 68 {N_("Buddy logs in"), OPT_SOUND_LOGIN, "arrive.wav"}, |
| 69 {N_("Buddy logs out"), OPT_SOUND_LOGOUT, "leave.wav"}, | |
| 4012 | 70 {N_("Message received"), OPT_SOUND_RECV, "receive.wav"}, |
| 71 {N_("Message received begins conversation"), OPT_SOUND_FIRST_RCV, "receive.wav"}, | |
| 72 {N_("Message sent"), OPT_SOUND_SEND, "send.wav"}, | |
| 4013 | 73 {N_("Person enters chat"), OPT_SOUND_CHAT_JOIN, "arrive.wav"}, |
| 74 {N_("Person leaves chat"), OPT_SOUND_CHAT_PART, "leave.wav"}, | |
| 4012 | 75 {N_("You talk in chat"), OPT_SOUND_CHAT_YOU_SAY, "send.wav"}, |
| 76 {N_("Others talk in chat"), OPT_SOUND_CHAT_SAY, "receive.wav"}, | |
| 3326 | 77 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ |
| 4012 | 78 {NULL, 0, "redalert.wav"}, |
| 79 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"} | |
| 3060 | 80 }; |
| 81 | |
| 4430 | 82 #ifndef _WIN32 |
| 83 static int check_dev(char *dev) | |
| 84 { | |
| 85 struct stat stat_buf; | |
| 86 uid_t user = getuid(); | |
| 87 gid_t group = getgid(), other_groups[32]; | |
| 88 int i, numgroups; | |
| 89 | |
| 90 if ((numgroups = getgroups(32, other_groups)) == -1) | |
| 91 return 0; | |
| 92 if (stat(dev, &stat_buf)) | |
| 93 return 0; | |
| 94 if (user == stat_buf.st_uid && stat_buf.st_mode & S_IWUSR) | |
| 95 return 1; | |
| 96 if (stat_buf.st_mode & S_IWGRP) { | |
| 97 if (group == stat_buf.st_gid) | |
| 98 return 1; | |
| 99 for (i = 0; i < numgroups; i++) | |
| 100 if (other_groups[i] == stat_buf.st_gid) | |
| 101 return 1; | |
| 102 } | |
| 103 if (stat_buf.st_mode & S_IWOTH) | |
| 104 return 1; | |
| 105 return 0; | |
| 106 } | |
| 107 | |
| 108 static void play_audio_file(char *file) | |
| 109 { | |
| 110 /* here we can assume that we can write to /dev/audio */ | |
| 111 char *buf; | |
| 112 struct stat info; | |
| 113 int fd = open(file, O_RDONLY); | |
| 114 if (fd <= 0) { | |
| 115 return; | |
| 116 } | |
| 117 fstat(fd, &info); | |
| 118 if (info.st_size < 24) | |
| 119 return; | |
| 120 buf = malloc(info.st_size + 1); | |
| 121 read(fd, buf, 24); | |
| 122 read(fd, buf, info.st_size - 24); | |
| 123 close(fd); | |
| 124 | |
| 125 fd = open("/dev/audio", O_WRONLY | O_EXCL | O_NDELAY); | |
| 126 if (fd < 0) { | |
| 127 free(buf); | |
| 128 return; | |
| 129 } | |
| 130 write(fd, buf, info.st_size - 24); | |
| 131 free(buf); | |
| 132 close(fd); | |
| 133 } | |
| 134 | |
| 135 static int can_play_audio() | |
| 136 { | |
| 137 return check_dev("/dev/audio"); | |
| 138 } | |
| 139 | |
| 140 #ifdef ARTSC_SOUND | |
| 141 | |
| 142 /* | |
| 143 ** This routine converts from ulaw to 16 bit linear. | |
| 144 ** | |
| 145 ** Craig Reese: IDA/Supercomputing Research Center | |
| 146 ** 29 September 1989 | |
| 147 ** | |
| 148 ** References: | |
| 149 ** 1) CCITT Recommendation G.711 (very difficult to follow) | |
| 150 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards | |
| 151 ** for Analog-to_Digital Conversion Techniques," | |
| 152 ** 17 February 1987 | |
| 153 ** | |
| 154 ** Input: 8 bit ulaw sample | |
| 155 ** Output: signed 16 bit linear sample | |
| 156 ** Z-note -- this is from libaudiofile. Thanks guys! | |
| 157 */ | |
| 158 | |
| 159 static int _af_ulaw2linear(unsigned char ulawbyte) | |
| 160 { | |
| 161 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 }; | |
| 162 int sign, exponent, mantissa, sample; | |
| 163 | |
| 164 ulawbyte = ~ulawbyte; | |
| 165 sign = (ulawbyte & 0x80); | |
| 166 exponent = (ulawbyte >> 4) & 0x07; | |
| 167 mantissa = ulawbyte & 0x0F; | |
| 168 sample = exp_lut[exponent] + (mantissa << (exponent + 3)); | |
| 169 if (sign != 0) | |
| 170 sample = -sample; | |
| 171 | |
| 172 return (sample); | |
| 173 } | |
| 174 | |
| 175 static int play_artsc(unsigned char *data, int size) | |
| 176 { | |
| 177 arts_stream_t stream; | |
| 178 guint16 *lineardata; | |
| 179 int result = 1; | |
| 180 int error; | |
| 181 int i; | |
| 182 | |
| 183 lineardata = g_malloc(size * 2); | |
| 184 | |
| 185 for (i = 0; i < size; i++) { | |
| 186 lineardata[i] = _af_ulaw2linear(data[i]); | |
| 187 } | |
| 188 | |
| 189 stream = arts_play_stream(8012, 16, 1, "gaim"); | |
| 190 | |
| 191 error = arts_write(stream, lineardata, size); | |
| 192 if (error < 0) { | |
| 193 result = 0; | |
| 194 } | |
| 195 | |
| 196 arts_close_stream(stream); | |
| 197 | |
| 198 g_free(lineardata); | |
| 199 | |
| 200 arts_free(); | |
| 201 | |
| 202 return result; | |
| 203 } | |
| 204 | |
| 205 static int can_play_artsc() | |
| 206 { | |
| 207 int error; | |
| 208 | |
| 209 error = arts_init(); | |
| 210 if (error < 0) | |
| 211 return 0; | |
| 212 | |
| 213 return 1; | |
| 214 } | |
| 215 | |
| 216 static int artsc_play_file(char *file) | |
| 217 { | |
| 218 struct stat stat_buf; | |
| 219 unsigned char *buf = NULL; | |
| 220 int result = 0; | |
| 221 int fd = -1; | |
| 222 | |
| 223 if (!can_play_artsc()) | |
| 224 return 0; | |
| 225 | |
| 226 fd = open(file, O_RDONLY); | |
| 227 if (fd < 0) | |
| 228 return 0; | |
| 229 | |
| 230 if (fstat(fd, &stat_buf)) { | |
| 231 close(fd); | |
| 232 return 0; | |
| 233 } | |
| 234 | |
| 235 if (!stat_buf.st_size) { | |
| 236 close(fd); | |
| 237 return 0; | |
| 238 } | |
| 239 | |
| 240 buf = g_malloc(stat_buf.st_size); | |
| 241 if (!buf) { | |
| 242 close(fd); | |
| 243 return 0; | |
| 244 } | |
| 245 | |
| 246 if (read(fd, buf, stat_buf.st_size) < 0) { | |
| 247 g_free(buf); | |
| 248 close(fd); | |
| 249 return 0; | |
| 250 } | |
| 251 | |
| 252 result = play_artsc(buf, stat_buf.st_size); | |
| 253 | |
| 254 g_free(buf); | |
| 255 close(fd); | |
| 256 return result; | |
| 257 } | |
| 258 | |
| 259 #endif /* ARTSC_SOUND */ | |
| 260 | |
| 261 #ifdef NAS_SOUND | |
| 262 | |
| 263 char nas_server[] = "localhost"; | |
| 264 AuServer *nas_serv = NULL; | |
| 265 | |
| 266 static AuBool NasEventHandler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * handler) | |
| 267 { | |
| 268 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev; | |
| 269 | |
| 270 if (ev->type == AuEventTypeElementNotify) { | |
| 271 switch (event->kind) { | |
| 272 case AuElementNotifyKindState: | |
| 273 switch (event->cur_state) { | |
| 274 case AuStateStop: | |
| 275 _exit(0); | |
| 276 } | |
| 277 break; | |
| 278 } | |
| 279 } | |
| 280 return AuTrue; | |
| 281 } | |
| 282 | |
| 283 | |
| 284 static int play_nas(unsigned char *data, int size) | |
| 285 { | |
| 286 AuDeviceID device = AuNone; | |
| 287 AuFlowID flow; | |
| 288 AuElement elements[3]; | |
| 289 int i, n, w; | |
| 290 | |
| 291 /* look for an output device */ | |
| 292 for (i = 0; i < AuServerNumDevices(nas_serv); i++) { | |
| 293 if ((AuDeviceKind(AuServerDevice(nas_serv, i)) == | |
| 294 AuComponentKindPhysicalOutput) && | |
| 295 AuDeviceNumTracks(AuServerDevice(nas_serv, i)) == 1) { | |
| 296 device = AuDeviceIdentifier(AuServerDevice(nas_serv, i)); | |
| 297 break; | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 if (device == AuNone) | |
| 302 return 0; | |
| 303 | |
| 304 if (!(flow = AuCreateFlow(nas_serv, NULL))) | |
| 305 return 0; | |
| 306 | |
| 307 | |
| 308 AuMakeElementImportClient(&elements[0], 8012, AuFormatULAW8, 1, AuTrue, size, size / 2, 0, NULL); | |
| 309 AuMakeElementExportDevice(&elements[1], 0, device, 8012, AuUnlimitedSamples, 0, NULL); | |
| 310 AuSetElements(nas_serv, flow, AuTrue, 2, elements, NULL); | |
| 311 | |
| 312 AuStartFlow(nas_serv, flow, NULL); | |
| 313 | |
| 314 AuWriteElement(nas_serv, flow, 0, size, data, AuTrue, NULL); | |
| 315 | |
| 316 AuRegisterEventHandler(nas_serv, AuEventHandlerIDMask, 0, flow, NasEventHandler, NULL); | |
| 317 | |
| 318 while (1) { | |
| 319 AuHandleEvents(nas_serv); | |
| 320 } | |
| 321 | |
| 322 return 1; | |
| 323 } | |
| 324 | |
| 325 static int can_play_nas() | |
| 326 { | |
| 327 if ((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) | |
| 328 return 1; | |
| 329 return 0; | |
| 330 } | |
| 331 | |
| 332 static int play_nas_file(char *file) | |
| 333 { | |
| 334 struct stat stat_buf; | |
| 335 char *buf; | |
| 336 int ret; | |
| 337 int fd = open(file, O_RDONLY); | |
| 338 if (fd <= 0) | |
| 339 return 0; | |
| 340 | |
| 341 if (!can_play_nas()) | |
| 342 return 0; | |
| 343 | |
| 344 if (stat(file, &stat_buf)) | |
| 345 return 0; | |
| 346 | |
| 347 if (!stat_buf.st_size) | |
| 348 return 0; | |
| 349 | |
| 350 buf = malloc(stat_buf.st_size); | |
| 351 read(fd, buf, stat_buf.st_size); | |
| 352 ret = play_nas(buf, stat_buf.st_size); | |
| 353 free(buf); | |
| 354 return ret; | |
| 355 } | |
| 356 | |
| 357 #endif /* NAS_SOUND */ | |
| 358 | |
| 359 #endif /* !_WIN32 */ | |
| 360 | |
|
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
361 void play_file(char *filename) |
|
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
362 { |
| 3630 | 363 #ifndef _WIN32 |
| 4430 | 364 int pid; |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
365 #endif |
| 3901 | 366 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY)) |
| 367 return; /* check here in case a buddy pounce plays a file while away */ | |
| 4430 | 368 |
| 3004 | 369 if (sound_options & OPT_SOUND_BEEP) { |
| 370 gdk_beep(); | |
| 371 return; | |
| 372 } | |
| 373 | |
| 4430 | 374 else if (sound_options & OPT_SOUND_NORMAL) { |
| 375 debug_printf("attempting to play audio file with internal method -- this is unlikely to work\n"); | |
| 3060 | 376 } |
| 4430 | 377 #ifndef _WIN32 |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
378 pid = fork(); |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
379 |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
380 if (pid < 0) |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
381 return; |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
382 else if (pid == 0) { |
| 4430 | 383 alarm(30); |
| 384 | |
| 385 if ((sound_options & OPT_SOUND_CMD) && sound_cmd[0]) { | |
| 386 char *args[4]; | |
| 387 char command[4096]; | |
| 4429 | 388 |
| 4430 | 389 g_snprintf(command, sizeof(command), sound_cmd, filename); |
| 4429 | 390 |
| 4430 | 391 args[0] = "sh"; |
| 392 args[1] = "-c"; | |
| 393 args[2] = command; | |
| 394 args[3] = NULL; | |
| 395 execvp(args[0], args); | |
| 396 _exit(0); | |
| 397 } | |
| 398 #ifdef ESD_SOUND | |
| 399 else if (sound_options & OPT_SOUND_ESD) { | |
| 400 if (esd_play_file(NULL, filename, 1)) | |
| 401 _exit(0); | |
| 402 } | |
| 403 #endif | |
| 4429 | 404 |
| 4430 | 405 #ifdef ARTSC_SOUND |
| 406 else if (sound_options & OPT_SOUND_ARTSC) { | |
| 407 if (artsc_play_file(filename)) | |
| 408 _exit(0); | |
| 409 } | |
| 410 #endif | |
| 4429 | 411 |
| 4430 | 412 #ifdef NAS_SOUND |
| 413 else if (sound_options & OPT_SOUND_NAS) { | |
| 414 if (play_nas_file(filename)) | |
| 415 _exit(0); | |
|
2580
86eaeb064e82
[gaim-migrate @ 2593]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2478
diff
changeset
|
416 } |
| 4430 | 417 #endif |
| 418 | |
| 419 else if ((sound_options & OPT_SOUND_NORMAL) && | |
| 420 can_play_audio()) { | |
| 421 play_audio_file(filename); | |
| 422 _exit(0); | |
| 423 } | |
| 424 | |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
425 _exit(0); |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
426 } |
| 3630 | 427 #else /* _WIN32 */ |
| 428 debug_printf("Playing %s\n", filename); | |
| 429 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) | |
| 430 debug_printf("Error playing sound."); | |
| 431 #endif | |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
432 } |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
433 |
|
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
434 extern int logins_not_muted; |
| 1 | 435 |
| 436 void play_sound(int sound) | |
| 437 { | |
| 3319 | 438 if (mute_sounds) |
| 439 return; | |
| 4430 | 440 |
| 3060 | 441 if ((sound == SND_BUDDY_ARRIVE) && !logins_not_muted) |
| 442 return; | |
| 443 | |
| 444 if (sound >= NUM_SOUNDS) { | |
| 4082 | 445 debug_printf("got request for unknown sound: %d\n", sound); |
| 3060 | 446 return; |
| 447 } | |
| 448 | |
| 449 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
| 3072 | 450 if ((sound_options & sounds[sound].opt) || (sounds[sound].opt == 0)) { |
| 3060 | 451 if (sound_file[sound]) { |
| 452 play_file(sound_file[sound]); | |
| 453 } else { | |
| 4004 | 454 gchar *filename = NULL; |
| 455 | |
| 456 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[sound].def, NULL); | |
| 457 play_file(filename); | |
| 458 g_free(filename); | |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
459 } |
|
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
460 } |
| 64 | 461 } |
