$OpenBSD: patch-platforms_unix_vm-sound-OpenBSD_sqUnixSoundOpenBSD_c,v 1.1 2008/07/23 11:17:22 espie Exp $
--- platforms/unix/vm-sound-OpenBSD/sqUnixSoundOpenBSD.c.orig	Wed Jul 23 13:05:41 2008
+++ platforms/unix/vm-sound-OpenBSD/sqUnixSoundOpenBSD.c	Wed Jul 23 13:05:12 2008
@@ -1,4 +1,4 @@
-/* sqUnixSoundSun.c -- sound support for SunOS and Solaris
+/* sqUnixSoundSun.c -- sound support for OpenBSD
  *
  *   Copyright (C) 1996-2004 by Ian Piumarta and other authors/contributors
  *                              listed elsewhere in this file.
@@ -24,10 +24,8 @@
  *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  *   SOFTWARE.
  *
- * Authors: Ian.Piumarta@inria.fr and Lex Spoon <lex@cc.gatech.edu>
- * 
- * This support is rudimentary and is implemented largely by reading
- * header files and guessing what to do.
+ * Authors: Marc.Espie@openbsd.org, Ian.Piumarta@inria.fr and 
+ * Lex Spoon <lex@cc.gatech.edu>
  */
 
 #include "sq.h"
@@ -40,17 +38,13 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
-#ifdef HAVE_SYS_AUDIOIO_H
-# include <sys/audioio.h>
-#else
-# include <sun/audioio.h>
-#endif
+#include <sys/audioio.h>
 #include <errno.h>
 
 #ifdef DEBUG
 # define PRINTF(ARGS) printf ARGS
 # define TRACE 1
-#elsen
+#else
 # define PRINTF(ARGS)
 #endif
 
@@ -62,9 +56,14 @@ static int auPlaySemaIndex=	0;   /* an index to signal
 static int auBufBytes=		0;   /*  buffer size to use for playback.
 					 unfortunately, this bears no relationship to
 					 whatever the kernel and soundcard are using  */
-static int auBuffersPlayed=	0;
 
+static unsigned int hwbufSize;
+static unsigned int threshold;
+static unsigned long totalWritten = 0;
 
+static int sound_AvailableSpace(void);
+
+
 static void auHandle(int fd, void *data, int flags)
 {
   if (auFd < 0) return;
@@ -84,6 +83,7 @@ static int sound_Stop(void)
   aioDisable(auFd);
   close(auFd);
   auFd= -1;
+  totalWritten = 0;
 
   return 0;
 }
@@ -100,35 +100,36 @@ static int sound_Start(int frameCount, int samplesPerS
   auPlaySemaIndex= semaIndex;
   fmtStereo= stereo;
   auBufBytes= bytesPerFrame * frameCount;
+  totalWritten = 0;
 
   if ((auFd= open("/dev/audio", O_WRONLY)) == -1)
     {
       perror("/dev/audio");
       return false;
     }
-  /* set up device */
-  if (ioctl(auFd, AUDIO_GETINFO, &info))
-    {
-      perror("AUDIO_GETINFO");
-      goto closeAndFail;
-    }
-  info.play.gain= 100;
+  AUDIO_INITINFO(&info);
   info.play.precision= 16;
-  info.play.encoding= AUDIO_ENCODING_LINEAR;
+  info.play.encoding= AUDIO_ENCODING_SLINEAR;
   info.play.channels= fmtStereo ? 2 : 1;
   info.play.sample_rate= samplesPerSec;
 
-  auBuffersPlayed= info.play.eof;
-
-  while ((err= ioctl(auFd, AUDIO_SETINFO, &info)) && errno == EINTR)
-    ;
-  if (err)
+  if (ioctl(auFd, AUDIO_SETINFO, &info))
     {
       perror("AUDIO_SETINFO");
       goto closeAndFail;
     }
+  if (ioctl(auFd, AUDIO_GETINFO, &info))
+    {
+      perror("AUDIO_GETINFO");
+      goto closeAndFail;
+    }
+  hwbufSize = info.play.buffer_size;
+  threshold = (hwbufSize * 3) / 4;
+
   aioEnable(auFd, 0, 0);
   aioHandle(auFd, auHandle, AIO_RX);
+
+
   return true;
   
  closeAndFail:
@@ -141,6 +142,7 @@ static int sound_Start(int frameCount, int samplesPerS
 static int sound_AvailableSpace(void)
 {
   struct audio_info info;
+  unsigned long fill;
 
   if (auFd < 0) return 0;
 
@@ -149,7 +151,15 @@ static int sound_AvailableSpace(void)
       perror("AUDIO_GETINFO");
       sound_Stop();
     }
-  return (auBufBytes * (info.play.eof - auBuffersPlayed + 2));
+  fill = totalWritten - info.play.samples;
+  if (fill >= threshold)
+    {
+      return 0;
+    }
+  else
+    {
+      return threshold - fill;
+    }
 }
 
 
@@ -188,10 +198,8 @@ static int sound_PlaySamplesFromAtLength(int frameCoun
 	}
       bytes -= len;
       pos += len;
+      totalWritten += len;
     }
-  /* add an eof marker */
-  write(auFd, buf, 0);
-  auBuffersPlayed += 1;
   
   return frameCount;
 }
@@ -263,7 +271,7 @@ static void sound_SetVolume(double left, double right)
 
 #include "SqSound.h"
 
-SqSoundDefine(Sun);
+SqSoundDefine(OpenBSD);
 
 
 #include "SqModule.h"
@@ -272,6 +280,6 @@ static void  sound_parseEnvironment(void) {}
 static int   sound_parseArgument(int argc, char **argv) { return 0; }
 static void  sound_printUsage(void) {}
 static void  sound_printUsageNotes(void) {}
-static void *sound_makeInterface(void) { return &sound_Sun_itf; }
+static void *sound_makeInterface(void) { return &sound_OpenBSD_itf; }
 
-SqModuleDefine(sound, Sun);
+SqModuleDefine(sound, OpenBSD);
