1 /*
2  * sound.c - Platform Independant Sound Support - Apr. 1995
3  *
4  * Copyright 1994-1995 Sujal M. Patel (smpatel@wam.umd.edu)
5  * Conditions in "copyright.h"
6  */
7 
8 #include <config.h>
9 
10 #ifdef SOUND
11 
12 #include <stdio.h>
13 #ifdef STDC_HEADERS
14 # include <stdlib.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 # include <unistd.h>
18 #endif
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
21 #endif
22 #ifdef HAVE_FCNTL_H
23 # include <fcntl.h>
24 #endif
25 #include <signal.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include "struct.h"
29 #include "data.h"
30 
31 static int soundfd;
32 static char audioOK = 1;
33 static char sound_flags[20]; /* Sound Flag for sound 1-19 */
34 
35 
init_sound()36 void init_sound ()
37 {
38   int i, child,fd[2];
39   char *argv[4];
40   char filename[512];
41 
42   signal(SIGPIPE, SIG_IGN);
43   signal(SIGCHLD, SIG_IGN);
44 
45   /* Do not initialize sound if it is not going to be used -- JEH */
46   if (! playSounds)
47 	  return;
48 
49   if(unixSoundPath[0] == '?')  {
50       audioOK = 0;
51       return;
52   };
53 
54   /*  Create a pipe, set the write end to close when we exec the sound server,
55       and set both (is the write end necessary?) ends to non-blocking   */
56   if (pipe(fd)) {
57       audioOK = 0;
58       return;
59   }
60   soundfd=fd[1];
61 
62   if( !(child=fork()) )  {
63       close(fd[1]);
64       dup2(fd[0],STDIN_FILENO);
65       close(fd[0]);
66       sprintf (filename, SOUNDSERVER);
67       argv[0] = filename;
68       argv[1] = unixSoundPath;
69       argv[2] = unixSoundDev;
70       argv[3] = NULL;
71       execvp(filename, argv);
72       fprintf (stderr, "Couldn't Execute Sound Server %s!\n", filename);
73       exit (0);
74   };
75   close(fd[0]);
76 
77   sleep(1);
78 
79   if (kill(child, 0))  {
80       audioOK = 0;
81       close(soundfd);
82   };
83 
84   for (i = 0; i < 19; i++) sound_flags[i] = 0;
85 }
86 
play_sound(k)87 void play_sound (k)
88 int k;
89 {
90   char c;
91 
92   c = k;
93   if ((playSounds) && (audioOK))
94     if(write (soundfd, &c, sizeof (c)) != sizeof (c))
95       audioOK = 0;
96 }
97 
98 
99 
maybe_play_sound(k)100 void maybe_play_sound (k)
101 int k;
102 {
103   char c;
104 
105   if (sound_flags[k] & 1) return;
106 
107   sound_flags[k] |= 1;
108 
109   c = (unsigned char)(k);
110   if ((playSounds) && (audioOK))
111     if(write (soundfd, &c, sizeof (c)) != sizeof (c))
112       audioOK = 0;
113 }
114 
115 
116 
sound_completed(k)117 void sound_completed (k)
118 int k;
119 {
120   sound_flags[k] &= ~1;
121 }
122 
123 
124 
kill_sound()125 void kill_sound ()
126 {
127   char c;
128 
129   c = -1;
130   if ((playSounds) && (audioOK))
131     if(write (soundfd, &c, sizeof (c)) != sizeof (c))
132       audioOK = 0;
133 }
134 
135 #endif /* SOUND */
136