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