Mercurial > emacs
comparison src/sound.c @ 70363:54e4c6ece57f
(Fplay_sound_internal): Dynamically allocate
current_sound_device and current_sound.
(sound_cleanup): Free them.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 03 May 2006 23:26:40 +0000 |
parents | 3bd95f4f2941 |
children | f8a3f870d0c4 146cd8369025 |
comparison
equal
deleted
inserted
replaced
70362:017a2a1beeee | 70363:54e4c6ece57f |
---|---|
450 | 450 |
451 static Lisp_Object | 451 static Lisp_Object |
452 sound_cleanup (arg) | 452 sound_cleanup (arg) |
453 Lisp_Object arg; | 453 Lisp_Object arg; |
454 { | 454 { |
455 if (current_sound_device) | 455 if (current_sound_device->close) |
456 { | 456 current_sound_device->close (current_sound_device); |
457 if (current_sound_device->close) | 457 if (current_sound->fd > 0) |
458 current_sound_device->close (current_sound_device); | 458 emacs_close (current_sound->fd); |
459 if (current_sound->fd > 0) | 459 free (current_sound_device); |
460 emacs_close (current_sound->fd); | 460 free (current_sound); |
461 } | |
462 | 461 |
463 return Qnil; | 462 return Qnil; |
464 } | 463 } |
465 | 464 |
466 /*********************************************************************** | 465 /*********************************************************************** |
989 int count = SPECPDL_INDEX (); | 988 int count = SPECPDL_INDEX (); |
990 | 989 |
991 #ifndef WINDOWSNT | 990 #ifndef WINDOWSNT |
992 Lisp_Object file; | 991 Lisp_Object file; |
993 struct gcpro gcpro1, gcpro2; | 992 struct gcpro gcpro1, gcpro2; |
994 struct sound_device sd; | |
995 struct sound s; | |
996 Lisp_Object args[2]; | 993 Lisp_Object args[2]; |
997 #else /* WINDOWSNT */ | 994 #else /* WINDOWSNT */ |
998 int len = 0; | 995 int len = 0; |
999 Lisp_Object lo_file = {0}; | 996 Lisp_Object lo_file = {0}; |
1000 char * psz_file = NULL; | 997 char * psz_file = NULL; |
1008 error ("Invalid sound specification"); | 1005 error ("Invalid sound specification"); |
1009 | 1006 |
1010 #ifndef WINDOWSNT | 1007 #ifndef WINDOWSNT |
1011 file = Qnil; | 1008 file = Qnil; |
1012 GCPRO2 (sound, file); | 1009 GCPRO2 (sound, file); |
1013 bzero (&sd, sizeof sd); | 1010 current_sound_device = (struct sound_device *) xmalloc (sizeof (struct sound_device)); |
1014 bzero (&s, sizeof s); | 1011 bzero (current_sound_device, sizeof (struct sound_device)); |
1015 current_sound_device = &sd; | 1012 current_sound = (struct sound *) xmalloc (sizeof (struct sound)); |
1016 current_sound = &s; | 1013 bzero (current_sound, sizeof (struct sound)); |
1017 record_unwind_protect (sound_cleanup, Qnil); | 1014 record_unwind_protect (sound_cleanup, Qnil); |
1018 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES); | 1015 current_sound->header = (char *) alloca (MAX_SOUND_HEADER_BYTES); |
1019 | 1016 |
1020 if (STRINGP (attrs[SOUND_FILE])) | 1017 if (STRINGP (attrs[SOUND_FILE])) |
1021 { | 1018 { |
1022 /* Open the sound file. */ | 1019 /* Open the sound file. */ |
1023 s.fd = openp (Fcons (Vdata_directory, Qnil), | 1020 current_sound->fd = openp (Fcons (Vdata_directory, Qnil), |
1024 attrs[SOUND_FILE], Qnil, &file, Qnil); | 1021 attrs[SOUND_FILE], Qnil, &file, Qnil); |
1025 if (s.fd < 0) | 1022 if (current_sound->fd < 0) |
1026 sound_perror ("Could not open sound file"); | 1023 sound_perror ("Could not open sound file"); |
1027 | 1024 |
1028 /* Read the first bytes from the file. */ | 1025 /* Read the first bytes from the file. */ |
1029 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES); | 1026 current_sound->header_size |
1030 if (s.header_size < 0) | 1027 = emacs_read (current_sound->fd, current_sound->header, |
1028 MAX_SOUND_HEADER_BYTES); | |
1029 if (current_sound->header_size < 0) | |
1031 sound_perror ("Invalid sound file header"); | 1030 sound_perror ("Invalid sound file header"); |
1032 } | 1031 } |
1033 else | 1032 else |
1034 { | 1033 { |
1035 s.data = attrs[SOUND_DATA]; | 1034 current_sound->data = attrs[SOUND_DATA]; |
1036 s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data)); | 1035 current_sound->header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (current_sound->data)); |
1037 bcopy (SDATA (s.data), s.header, s.header_size); | 1036 bcopy (SDATA (current_sound->data), current_sound->header, current_sound->header_size); |
1038 } | 1037 } |
1039 | 1038 |
1040 /* Find out the type of sound. Give up if we can't tell. */ | 1039 /* Find out the type of sound. Give up if we can't tell. */ |
1041 find_sound_type (&s); | 1040 find_sound_type (current_sound); |
1042 | 1041 |
1043 /* Set up a device. */ | 1042 /* Set up a device. */ |
1044 if (STRINGP (attrs[SOUND_DEVICE])) | 1043 if (STRINGP (attrs[SOUND_DEVICE])) |
1045 { | 1044 { |
1046 int len = SCHARS (attrs[SOUND_DEVICE]); | 1045 int len = SCHARS (attrs[SOUND_DEVICE]); |
1047 sd.file = (char *) alloca (len + 1); | 1046 current_sound_device->file = (char *) alloca (len + 1); |
1048 strcpy (sd.file, SDATA (attrs[SOUND_DEVICE])); | 1047 strcpy (current_sound_device->file, SDATA (attrs[SOUND_DEVICE])); |
1049 } | 1048 } |
1050 | 1049 |
1051 if (INTEGERP (attrs[SOUND_VOLUME])) | 1050 if (INTEGERP (attrs[SOUND_VOLUME])) |
1052 sd.volume = XFASTINT (attrs[SOUND_VOLUME]); | 1051 current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]); |
1053 else if (FLOATP (attrs[SOUND_VOLUME])) | 1052 else if (FLOATP (attrs[SOUND_VOLUME])) |
1054 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100; | 1053 current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100; |
1055 | 1054 |
1056 args[0] = Qplay_sound_functions; | 1055 args[0] = Qplay_sound_functions; |
1057 args[1] = sound; | 1056 args[1] = sound; |
1058 Frun_hook_with_args (2, args); | 1057 Frun_hook_with_args (2, args); |
1059 | 1058 |
1060 /* There is only one type of device we currently support, the VOX | 1059 /* There is only one type of device we currently support, the VOX |
1061 sound driver. Set up the device interface functions for that | 1060 sound driver. Set up the device interface functions for that |
1062 device. */ | 1061 device. */ |
1063 vox_init (&sd); | 1062 vox_init (current_sound_device); |
1064 | 1063 |
1065 /* Open the device. */ | 1064 /* Open the device. */ |
1066 sd.open (&sd); | 1065 current_sound_device->open (current_sound_device); |
1067 | 1066 |
1068 /* Play the sound. */ | 1067 /* Play the sound. */ |
1069 s.play (&s, &sd); | 1068 current_sound->play (current_sound, current_sound_device); |
1070 | |
1071 /* Close the input file, if any. */ | |
1072 if (!STRINGP (s.data)) | |
1073 { | |
1074 emacs_close (s.fd); | |
1075 s.fd = -1; | |
1076 } | |
1077 | |
1078 /* Close the device. */ | |
1079 sd.close (&sd); | |
1080 | 1069 |
1081 /* Clean up. */ | 1070 /* Clean up. */ |
1082 current_sound_device = NULL; | |
1083 current_sound = NULL; | |
1084 UNGCPRO; | 1071 UNGCPRO; |
1085 | 1072 |
1086 #else /* WINDOWSNT */ | 1073 #else /* WINDOWSNT */ |
1087 | 1074 |
1088 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil); | 1075 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil); |