home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / xgalaga-2_0_tar.gz / xgalaga-2_0_tar / xgalaga-2.0 / sound.c < prev    next >
C/C++ Source or Header  |  1998-04-12  |  2KB  |  123 lines

  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 "data.h"
  28.  
  29.  
  30.  
  31. static int soundfd;
  32. static char audioOK = 1;
  33. static char sound_flags[20]; /* Sound Flag for sound 1-19 */
  34.  
  35.  
  36.  
  37. void init_sound ()
  38. {
  39.   int i, child,fd[2];
  40.   char *argv[4];
  41.   char filename[512];
  42.  
  43.   signal(SIGCHLD, SIG_IGN);
  44.  
  45.   if(unixSoundPath[0] == '?')  {
  46.       audioOK = 0;
  47.       return;
  48.   };
  49.  
  50.   /*  Create a pipe, set the write end to close when we exec the sound server,
  51.       and set both (is the write end necessary?) ends to non-blocking   */
  52.   pipe(fd);
  53.   soundfd=fd[1];
  54.  
  55.   if( !(child=fork()) )  {
  56.       close(fd[1]);
  57.       dup2(fd[0],STDIN_FILENO);
  58.       close(fd[0]);
  59.       sprintf (filename, SOUNDSERVER);
  60.       argv[0] = filename;
  61.       argv[1] = unixSoundPath;
  62.       argv[2] = unixSoundDev;
  63.       argv[3] = NULL;
  64.       execvp(filename, argv);
  65.       fprintf (stderr, "Couldn't Execute Sound Server %s!\n", filename);
  66.       exit (0);
  67.   };
  68.   close(fd[0]);
  69.  
  70.   sleep(1);
  71.  
  72.   if (kill(child, 0))  {
  73.       audioOK = 0;  
  74.       close(soundfd);
  75.   };
  76.  
  77.   for (i = 0; i < 19; i++) sound_flags[i] = 0;
  78.  
  79. void play_sound (k)
  80. int k;
  81. {
  82.   char c;
  83.  
  84.   c = k;
  85.   if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
  86. }
  87.  
  88.  
  89.  
  90. void maybe_play_sound (k)
  91. int k;
  92. {
  93.   char c;
  94.  
  95.   if (sound_flags[k] & 1) return;
  96.  
  97.   sound_flags[k] |= 1;
  98.  
  99.   c = (unsigned char)(k);
  100.   if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
  101. }
  102.  
  103.  
  104.  
  105. void sound_completed (k)
  106. int k;
  107. {
  108.   sound_flags[k] &= ~1;
  109. }
  110.  
  111.  
  112.  
  113. void kill_sound ()
  114.   char c;
  115.  
  116.   c = -1;               
  117.   if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
  118. }
  119.  
  120. #endif /* SOUND */
  121.