Index: roard/driver_oss.c
===================================================================
--- roard/driver_oss.c	(revision 1242)
+++ roard/driver_oss.c	(revision 1518)
@@ -26,11 +26,41 @@
 #if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS)
 
-#define er() close(fh); return -1
-
-int driver_oss_open(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh) {
- int tmp;
- int ctmp;
- char * es;
- uint32_t tmp32;
+struct driver_oss {
+ char * device;
+ int fh;
+ int blocks;
+ int blocksize;
+ struct roar_audio_info info;
+};
+
+#define _get(vio,obj) (((struct driver_oss*)((vio)->inst))->obj)
+
+ssize_t driver_oss_write    (struct roar_vio_calls * vio, void *buf, size_t count);
+int     driver_oss_nonblock (struct roar_vio_calls * vio, int state);
+int     driver_oss_close_vio(struct roar_vio_calls * vio);
+
+int driver_oss_init_vio(struct roar_vio_calls * vio, struct driver_oss * inst) {
+ if ( vio == NULL )
+  return -1;
+
+ memset(vio, 0, sizeof(struct roar_vio_calls));
+
+ vio->write    = driver_oss_write;
+ vio->nonblock = driver_oss_nonblock;
+ vio->sync     = driver_oss_sync;
+ vio->ctl      = driver_oss_ctl;
+ vio->close    = driver_oss_close_vio;
+
+ vio->inst     = (void*) inst;
+
+ return 0;
+}
+
+int driver_oss_open_device(struct driver_oss * self) {
+ int    fh     = self->fh;
+ char * device = self->device;
+
+ if ( fh != -1 )
+  return 0;
 
 #ifdef ROAR_DEFAULT_OSS_DEV
@@ -40,22 +70,26 @@
 
  if ( device == NULL ) {
-  ROAR_ERR("driver_oss_open(*): no default device found, you need to specify one manuelly");
-  return -1;
- }
-
- roar_vio_init_calls(inst);
- inst->sync = driver_oss_sync;
- inst->ctl  = driver_oss_ctl;
-
- if (  fh == -1 ) {
-  if ( (fh = open(device, O_WRONLY, 0644)) == -1 ) {
-   ROAR_ERR("driver_oss_open(*): Can not open OSS device: %s: %s", device, strerror(errno));
-   return -1;
-  }
- }
-
- roar_vio_set_fh(inst, fh);
-
-
+  ROAR_ERR("driver_oss_open_device(*): no default device found, you need to specify one manuelly");
+  return -1;
+ }
+
+ if ( (fh = open(device, O_WRONLY, 0644)) == -1 ) {
+  ROAR_ERR("driver_oss_open_device(*): Can not open OSS device: %s: %s", device, strerror(errno));
+  return -1;
+ }
+
+ self->fh = fh;
+
+ return 0;
+}
+
+int driver_oss_config_device(struct driver_oss * self) {
+ int                      fh   =   self->fh;
+ struct roar_audio_info * info = &(self->info);
+ int tmp, ctmp;
+ char * es;
+
+ if ( fh == -1 )
+  return -1;
 
 #ifdef SNDCTL_DSP_CHANNELS
@@ -64,10 +98,10 @@
  if ( ioctl(fh, SNDCTL_DSP_CHANNELS, &tmp) == -1 ) {
   ROAR_ERR("driver_oss_open(*): can not set number of channels");
-  er();
+  return -1;
  }
 
  if ( tmp != info->channels ) {
    ROAR_ERR("driver_oss_open(*): can not set requested numer of channels, OSS suggested %i channels, to use this restart with -oO channels=%i or set codec manuelly via -oO channels=num", tmp, tmp);
-  er();
+  return -1;
  }
 #else
@@ -75,10 +109,10 @@
   case  1: tmp = 0; break;
   case  2: tmp = 1; break;
-  default: er();
+  default: return -1;
  }
 
  if ( ioctl(fh, SNDCTL_DSP_STEREO, &tmp) == -1 ) {
   ROAR_ERR("driver_oss_open(*): can not set number of channels");
-  er();
+  return -1;
  }
 #endif
@@ -93,5 +127,5 @@
      case 32: tmp = AFMT_S32_LE; break;
 #endif
-     default: er();
+     default: return -1;
     }
    break;
@@ -104,5 +138,5 @@
      case 32: tmp = AFMT_S32_BE; break;
 #endif
-     default: er();
+     default: return -1;
     }
    break;
@@ -111,5 +145,5 @@
      case  8: tmp = AFMT_U8;     break;
      case 16: tmp = AFMT_U16_LE; break;
-     default: er();
+     default: return -1;
     }
    break;
@@ -118,5 +152,5 @@
      case  8: tmp = AFMT_U8;     break;
      case 16: tmp = AFMT_U16_BE; break;
-     default: er();
+     default: return -1;
     }
   case ROAR_CODEC_ALAW:
@@ -132,5 +166,5 @@
 #endif
   default:
-    er();
+    return -1;
    break;
  }
@@ -143,5 +177,5 @@
 #endif
   ROAR_ERR("driver_oss_open(*): can not set sample format");
-  er();
+  return -1;
  }
 
@@ -173,5 +207,5 @@
    ROAR_ERR("driver_oss_open(*): can not set requested codec, set codec manuelly via -oO codec=somecodec");
   }
-  er();
+  return -1;
  }
 
@@ -180,12 +214,47 @@
  if ( ioctl(fh, SNDCTL_DSP_SPEED, &tmp) == -1 ) {
   ROAR_ERR("driver_oss_open(*): can not set sample rate");
-  er();
+  return -1;
  }
 
  if ( tmp < info->rate * 0.98 || tmp > info->rate * 1.02 ) {
   ROAR_ERR("driver_oss_open(*): sample rate out of acceptable accuracy");
+  return -1;
+ }
+
+ return 0;
+}
+
+#define er() close(self->fh); if ( self->device ) free(self->device); free(self); return -1
+int driver_oss_open(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh) {
+ struct driver_oss * self = NULL;
+ uint32_t tmp32;
+
+ if ( (self = malloc(sizeof(struct driver_oss))) == NULL ) {
+  ROAR_ERR("driver_oss_open(*): Can not malloc() instance data: %s", strerror(errno));
+  return -1;
+ }
+
+ memset(self, 0, sizeof(struct driver_oss));
+ memcpy(&(self->info), info, sizeof(struct roar_audio_info));
+
+ self->fh = fh;
+
+ if ( device != NULL )
+  self->device = strdup(device);
+
+ if ( driver_oss_init_vio(inst, self) == -1 ) {
+  ROAR_ERR("driver_oss_open(*): Can not init vio interface");
   er();
  }
 
+ if ( driver_oss_open_device(self) == -1 ) {
+  ROAR_ERR("driver_oss_open(*): Can not open audio device");
+  er();
+ }
+
+ if ( driver_oss_config_device(self) == -1 ) {
+  ROAR_ERR("driver_oss_open(*): Can not configure audio device");
+  er();
+ }
 
  tmp32 = 4;
@@ -196,4 +265,5 @@
  return 0;
 }
+#undef er
 
 int driver_oss_close(DRIVER_USERDATA_T   inst) {
@@ -201,7 +271,29 @@
 }
 
+int     driver_oss_close_vio(struct roar_vio_calls * vio) {
+ close(_get(vio,fh));
+
+ if ( _get(vio,device) != NULL )
+  free(_get(vio,device));
+
+ free(vio->inst);
+ return 0;
+}
+
+int     driver_oss_nonblock(struct roar_vio_calls * vio, int state) {
+ if ( roar_socket_nonblock(_get(vio,fh), state) == -1 )
+  return -1;
+
+ if ( state == ROAR_SOCKET_NONBLOCK )
+  return 0;
+
+ roar_vio_sync(vio);
+
+ return 0;
+}
+
 int driver_oss_sync(struct roar_vio_calls * vio) {
 #ifdef SNDCTL_DSP_SYNC
- return ioctl(roar_vio_get_fh(vio), SNDCTL_DSP_SYNC, NULL);
+ return ioctl(_get(vio,fh), SNDCTL_DSP_SYNC, NULL);
 #else
  return 0;
@@ -218,5 +310,5 @@
   case ROAR_VIO_CTL_GET_DELAY:
 #ifdef SNDCTL_DSP_GETODELAY
-    if ( ioctl(roar_vio_get_fh(vio), SNDCTL_DSP_GETODELAY, &d) == -1 )
+    if ( ioctl(_get(vio,fh), SNDCTL_DSP_GETODELAY, &d) == -1 )
      return -1;
 
@@ -231,5 +323,5 @@
 #ifdef SNDCTL_DSP_SETFRAGMENT
     d = (*(uint_least32_t *)data) << 16 | 11; // (*data) fragements of 2048 Bytes.
-    if ( ioctl(roar_vio_get_fh(vio), SNDCTL_DSP_SETFRAGMENT, &d) == -1 ) {
+    if ( ioctl(_get(vio,fh), SNDCTL_DSP_SETFRAGMENT, &d) == -1 ) {
      ROAR_WARN("driver_oss_ctl(*): Can not set fragment size, sorry :(");
     }
@@ -249,4 +341,8 @@
 }
 
+ssize_t driver_oss_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
+ return write(_get(vio,fh), buf, count);
+}
+
 #endif
 //ll
