]> code.delx.au - pulseaudio/blobdiff - src/utils/padsp.c
We need to have a callback when changing volume or we might deadlock.
[pulseaudio] / src / utils / padsp.c
index ed34ac8aba67d18237a37c32aee71272186e78ca..c765b6935030f0632971951e874fb9c64ec3ffdd 100644 (file)
@@ -1,20 +1,20 @@
 /* $Id$ */
 
 /***
-  This file is part of polypaudio.
+  This file is part of PulseAudio.
  
-  polypaudio is free software; you can redistribute it and/or modify
+  PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
-  polypaudio is distributed in the hope that it will be useful, but
+  PulseAudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
   You should have received a copy of the GNU Lesser General Public License
-  along with polypaudio; if not, write to the Free Software
+  along with PulseAudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
 ***/
@@ -36,6 +36,7 @@
 #include <pthread.h>
 #include <unistd.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <dlfcn.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <signal.h>
 
+#ifdef __linux__
 #include <linux/sockios.h>
+#endif
 
-#include <polyp/polypaudio.h>
-#include <polypcore/llist.h>
-#include <polypcore/gccmacro.h>
+#include <pulse/pulseaudio.h>
+#include <pulsecore/llist.h>
+#include <pulsecore/gccmacro.h>
 
 typedef enum {
     FD_INFO_MIXER,
-    FD_INFO_PLAYBACK
+    FD_INFO_STREAM,
 } fd_info_type_t;
 
 typedef struct fd_info fd_info;
@@ -71,14 +74,21 @@ struct fd_info {
 
     pa_threaded_mainloop *mainloop;
     pa_context *context;
-    pa_stream *stream;
+    pa_stream *play_stream;
+    pa_stream *rec_stream;
 
     pa_io_event *io_event;
+    pa_io_event_flags_t io_flags;
 
     void *buf;
+    size_t rec_offset;
 
     int operation_success;
 
+    pa_cvolume sink_volume, source_volume;
+    uint32_t sink_index, source_index;
+    int volume_modify_count;
+    
     PA_LLIST_FIELDS(fd_info);
 };
 
@@ -94,15 +104,25 @@ static int (*_ioctl)(int, int, void*) = NULL;
 static int (*_close)(int) = NULL;
 static int (*_open)(const char *, int, mode_t) = NULL;
 static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
+#ifdef HAVE_OPEN64
 static int (*_open64)(const char *, int, mode_t) = NULL;
 static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
+#endif
 static int (*_fclose)(FILE *f) = NULL;
+static int (*_access)(const char *, int) = NULL;
+
+/* dlsym() violates ISO C, so confide the breakage into this function to
+ * avoid warnings. */
+typedef void (*fnptr)(void);
+static inline fnptr dlsym_fn(void *handle, const char *symbol) {
+    return (fnptr) (long) dlsym(handle, symbol);
+}
 
 #define LOAD_IOCTL_FUNC() \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_ioctl)  \
-        _ioctl = (int (*)(int, int, void*)) dlsym(RTLD_NEXT, "ioctl"); \
+        _ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -110,7 +130,7 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_open) \
-        _open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open"); \
+        _open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -118,7 +138,7 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_open64) \
-        _open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64"); \
+        _open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -126,7 +146,15 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_close) \
-        _close = (int (*)(int)) dlsym(RTLD_NEXT, "close"); \
+        _close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
+    pthread_mutex_unlock(&func_mutex); \
+} while(0)
+
+#define LOAD_ACCESS_FUNC() \
+do { \
+    pthread_mutex_lock(&func_mutex); \
+    if (!_access) \
+        _access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -134,7 +162,7 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_fopen) \
-        _fopen = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen"); \
+        _fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -142,7 +170,7 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_fopen64) \
-        _fopen64 = (FILE* (*)(const char *, const char*)) dlsym(RTLD_NEXT, "fopen64"); \
+        _fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
@@ -150,21 +178,113 @@ do { \
 do { \
     pthread_mutex_lock(&func_mutex); \
     if (!_fclose) \
-        _fclose = (int (*)(FILE *)) dlsym(RTLD_NEXT, "fclose"); \
+        _fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
     pthread_mutex_unlock(&func_mutex); \
 } while(0)
 
-static void debug(const char *format, ...) PA_GCC_PRINTF_ATTR(1,2);
-
-static void debug(const char *format, ...) {
+#define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
+if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    goto label; \
+} \
+} while(0);
+
+#define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
+if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
+    !(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    goto label; \
+} \
+} while(0);
+
+#define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
+if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
+    !(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
+    goto label; \
+} \
+} while(0);
+
+static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
+
+#define DEBUG_LEVEL_ALWAYS             0
+#define DEBUG_LEVEL_NORMAL             1
+#define DEBUG_LEVEL_VERBOSE            2
+
+static void debug(int level, const char *format, ...) {
     va_list ap;
-    if (getenv("PADSP_DEBUG")) {
-        va_start(ap, format);
-        vfprintf(stderr, format, ap);
-        va_end(ap);
+    const char *dlevel_s;
+    int dlevel;
+
+    dlevel_s = getenv("PADSP_DEBUG");
+    if (!dlevel_s)
+        return;
+
+    dlevel = atoi(dlevel_s);
+
+    if (dlevel < level)
+        return;
+
+    va_start(ap, format);
+    vfprintf(stderr, format, ap);
+    va_end(ap);
+}
+
+static int padsp_disabled(void) {
+    static int *sym;
+    static int sym_resolved = 0;
+
+    /* If the current process has a symbol __padsp_disabled__ we use
+     * it to detect whether we should enable our stuff or not. A
+     * program needs to be compiled with -rdynamic for this to work!
+     * The symbol must be an int containing a three bit bitmask: bit 1
+     * -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
+     * emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
+     * of 7 disables padsp entirely. */
+    
+    pthread_mutex_lock(&func_mutex);
+    if (!sym_resolved) {
+        sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
+        sym_resolved = 1;
+
     }
+    pthread_mutex_unlock(&func_mutex);
+
+    if (!sym)
+        return 0;
+    
+    return *sym;
 }
 
+static int dsp_cloak_enable(void) {
+    if (padsp_disabled() & 1)
+        return 0;
+    
+    if (getenv("PADSP_NO_DSP"))
+        return 0;
+
+    return 1;
+}
+
+static int sndstat_cloak_enable(void) {
+    if (padsp_disabled() & 2)
+        return 0;
+
+    if (getenv("PADSP_NO_SNDSTAT"))
+        return 0;
+
+    return 1;
+}
+
+static int mixer_cloak_enable(void) {
+    if (padsp_disabled() & 4)
+        return 0;
+
+    if (getenv("PADSP_NO_MIXER"))
+        return 0;
+
+    return 1;
+}
 static pthread_key_t recursion_key;
 
 static void recursion_key_alloc(void) {
@@ -190,16 +310,21 @@ static void function_exit(void) {
 static void fd_info_free(fd_info *i) {
     assert(i);
 
-    debug(__FILE__": freeing fd info (fd=%i)\n", i->app_fd);
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
 
     dsp_drain(i);
     
     if (i->mainloop)
         pa_threaded_mainloop_stop(i->mainloop);
     
-    if (i->stream) {
-        pa_stream_disconnect(i->stream);
-        pa_stream_unref(i->stream);
+    if (i->play_stream) {
+        pa_stream_disconnect(i->play_stream);
+        pa_stream_unref(i->play_stream);
+    }
+
+    if (i->rec_stream) {
+        pa_stream_disconnect(i->rec_stream);
+        pa_stream_unref(i->rec_stream);
     }
 
     if (i->context) {
@@ -233,7 +358,7 @@ static fd_info *fd_info_ref(fd_info *i) {
     assert(i->ref >= 1);
     i->ref++;
 
-/*     debug(__FILE__": ref++, now %i\n", i->ref); */
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
     pthread_mutex_unlock(&i->mutex);
 
     return i;
@@ -244,7 +369,7 @@ static void fd_info_unref(fd_info *i) {
     pthread_mutex_lock(&i->mutex);
     assert(i->ref >= 1);
     r = --i->ref;
-/*     debug(__FILE__": ref--, now %i\n", i->ref); */
+       debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
     pthread_mutex_unlock(&i->mutex);
 
     if (r <= 0)
@@ -273,28 +398,41 @@ static void context_state_cb(pa_context *c, void *userdata) {
 static void reset_params(fd_info *i) {
     assert(i);
     
-    i->sample_spec.format = PA_SAMPLE_ULAW;
+    i->sample_spec.format = PA_SAMPLE_U8;
     i->sample_spec.channels = 1;
     i->sample_spec.rate = 8000;
-    i->fragment_size = 1024;
+    i->fragment_size = 0;
     i->n_fragments = 0;
 }
 
-static char *client_name(char *buf, size_t n) {
+static const char *client_name(char *buf, size_t n) {
     char p[PATH_MAX];
+    const char *e;
+
+    if ((e = getenv("PADSP_CLIENT_NAME")))
+        return e;
     
     if (pa_get_binary_name(p, sizeof(p)))
-        snprintf(buf, n, "oss[%s]", pa_path_get_filename(p));
+        snprintf(buf, n, "OSS Emulation[%s]", p);
     else
-        snprintf(buf, n, "oss");
+        snprintf(buf, n, "OSS");
 
     return buf;
 }
 
+static const char *stream_name(void) {
+    const char *e;
+
+    if ((e = getenv("PADSP_STREAM_NAME")))
+        return e;
+
+    return "Audio Stream";
+}
+
 static void atfork_prepare(void) {
     fd_info *i;
 
-    debug(__FILE__": atfork_prepare() enter\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
     
     function_enter();
 
@@ -308,13 +446,13 @@ static void atfork_prepare(void) {
     pthread_mutex_lock(&func_mutex);
 
     
-    debug(__FILE__": atfork_prepare() exit\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
 }
 
 static void atfork_parent(void) {
     fd_info *i;
     
-    debug(__FILE__": atfork_parent() enter\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
 
     pthread_mutex_unlock(&func_mutex);
 
@@ -327,13 +465,13 @@ static void atfork_parent(void) {
 
     function_exit();
     
-    debug(__FILE__": atfork_parent() exit\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
 }
 
 static void atfork_child(void) {
     fd_info *i;
     
-    debug(__FILE__": atfork_child() enter\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
 
     /* We do only the bare minimum to get all fds closed */
     pthread_mutex_init(&func_mutex, NULL);
@@ -348,9 +486,14 @@ static void atfork_child(void) {
             i->context = NULL;
         }
 
-        if (i->stream) {
-            pa_stream_unref(i->stream);
-            i->stream = NULL;
+        if (i->play_stream) {
+            pa_stream_unref(i->play_stream);
+            i->play_stream = NULL;
+        }
+
+        if (i->rec_stream) {
+            pa_stream_unref(i->rec_stream);
+            i->rec_stream = NULL;
         }
 
         if (i->app_fd >= 0) {
@@ -368,20 +511,40 @@ static void atfork_child(void) {
 
     function_exit();
 
-    debug(__FILE__": atfork_child() exit\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
 }
 
 static void install_atfork(void) {
     pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
 }
 
+static void stream_success_cb(pa_stream *s, int success, void *userdata) {
+    fd_info *i = userdata;
+
+    assert(s);
+    assert(i);
+
+    i->operation_success = success;
+    pa_threaded_mainloop_signal(i->mainloop, 0);
+}
+
+static void context_success_cb(pa_context *c, int success, void *userdata) {
+    fd_info *i = userdata;
+
+    assert(c);
+    assert(i);
+
+    i->operation_success = success;
+    pa_threaded_mainloop_signal(i->mainloop, 0);
+}
+
 static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
     fd_info *i;
     int sfds[2] = { -1, -1 };
     char name[64];
     static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
 
-    debug(__FILE__": fd_info_new()\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
 
     signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
 
@@ -397,19 +560,27 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 
     i->mainloop = NULL;
     i->context = NULL;
-    i->stream = NULL;
+    i->play_stream = NULL;
+    i->rec_stream = NULL;
     i->io_event = NULL;
+    i->io_flags = 0;
     pthread_mutex_init(&i->mutex, NULL);
     i->ref = 1;
     i->buf = NULL;
+    i->rec_offset = 0;
     i->unusable = 0;
+    pa_cvolume_reset(&i->sink_volume, 2);
+    pa_cvolume_reset(&i->source_volume, 2);
+    i->volume_modify_count = 0;
+    i->sink_index = (uint32_t) -1;
+    i->source_index = (uint32_t) -1;
     PA_LLIST_INIT(fd_info, i);
 
     reset_params(i);
 
     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
         *_errno = errno;
-        debug(__FILE__": socket() failed: %s\n", strerror(errno));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
         goto fail;
     }
 
@@ -418,13 +589,13 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 
     if (!(i->mainloop = pa_threaded_mainloop_new())) {
         *_errno = EIO;
-        debug(__FILE__": pa_threaded_mainloop_new() failed\n");
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
         goto fail;
     }
 
     if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
         *_errno = EIO;
-        debug(__FILE__": pa_context_new() failed\n");
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
         goto fail;
     }
 
@@ -432,7 +603,7 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 
     if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
         *_errno = ECONNREFUSED;
-        debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
@@ -440,7 +611,7 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 
     if (pa_threaded_mainloop_start(i->mainloop) < 0) {
         *_errno = EIO;
-        debug(__FILE__": pa_threaded_mainloop_start() failed\n");
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
         goto unlock_and_fail;
     }
 
@@ -449,7 +620,7 @@ static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
 
     if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
         *_errno = ECONNREFUSED;
-        debug(__FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto unlock_and_fail;
     }
 
@@ -509,17 +680,34 @@ static void fix_metrics(fd_info *i) {
     char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
 
     fs = pa_frame_size(&i->sample_spec);
+
+    /* Don't fix things more than necessary */
+    if ((i->fragment_size % fs) == 0 &&
+        i->n_fragments >= 2 &&
+        i->fragment_size > 0)
+        return;
+
     i->fragment_size = (i->fragment_size/fs)*fs;
-    
-    if (i->n_fragments < 2)
-        i->n_fragments = 12;
 
-    if (i->fragment_size <= 0)
-        if ((i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments) <= 0)
+    /* Number of fragments set? */
+    if (i->n_fragments < 2) {
+        if (i->fragment_size > 0) {
+            i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
+            if (i->n_fragments < 2)
+                i->n_fragments = 2;
+        } else
+            i->n_fragments = 12;
+    }
+
+    /* Fragment size set? */
+    if (i->fragment_size <= 0) {
+        i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
+        if (i->fragment_size < 1024)
             i->fragment_size = 1024;
+    }
 
-    debug(__FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
-    debug(__FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
 }
 
 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
@@ -528,8 +716,37 @@ static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
 
     if (i->io_event) {
         pa_mainloop_api *api;
+        size_t n;
+
         api = pa_threaded_mainloop_get_api(i->mainloop);
-        api->io_enable(i->io_event, PA_IO_EVENT_INPUT);
+
+        if (s == i->play_stream) {
+            n = pa_stream_writable_size(i->play_stream);
+            if (n == (size_t)-1) {
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
+                    pa_strerror(pa_context_errno(i->context)));
+            }
+
+            if (n >= i->fragment_size)
+                i->io_flags |= PA_IO_EVENT_INPUT;
+            else
+                i->io_flags &= ~PA_IO_EVENT_INPUT;
+        }
+
+        if (s == i->rec_stream) {
+            n = pa_stream_readable_size(i->rec_stream);
+            if (n == (size_t)-1) {
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
+                    pa_strerror(pa_context_errno(i->context)));
+            }
+
+            if (n >= i->fragment_size)
+                i->io_flags |= PA_IO_EVENT_OUTPUT;
+            else
+                i->io_flags &= ~PA_IO_EVENT_OUTPUT;
+        }
+
+        api->io_enable(i->io_event, i->io_flags);
     }
 }
 
@@ -548,6 +765,7 @@ static void fd_info_shutdown(fd_info *i) {
         api = pa_threaded_mainloop_get_api(i->mainloop);
         api->io_free(i->io_event);
         i->io_event = NULL;
+        i->io_flags = 0;
     }
 
     if (i->thread_fd >= 0) {
@@ -559,48 +777,114 @@ static void fd_info_shutdown(fd_info *i) {
 static int fd_info_copy_data(fd_info *i, int force) {
     size_t n;
 
-    if (!i->stream)
+    if (!i->play_stream && !i->rec_stream)
         return -1;
 
-    if ((n = pa_stream_writable_size(i->stream)) == (size_t) -1) {
-        debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
-        return -1;
-    }
-    
-    while (n >= i->fragment_size || force) {
-        ssize_t r;
-        
-        if (!i->buf) {
-            if (!(i->buf = malloc(i->fragment_size))) {
-                debug(__FILE__": malloc() failed.\n");
+    if ((i->play_stream) && (pa_stream_get_state(i->play_stream) == PA_STREAM_READY)) {
+        n = pa_stream_writable_size(i->play_stream);
+
+        if (n == (size_t)-1) {
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
+                pa_strerror(pa_context_errno(i->context)));
+            return -1;
+        }
+
+        while (n >= i->fragment_size || force) {
+            ssize_t r;
+
+            if (!i->buf) {
+                if (!(i->buf = malloc(i->fragment_size))) {
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
+                    return -1;
+                }
+            }
+
+            if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
+
+                if (errno == EAGAIN)
+                    break;
+
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
                 return -1;
             }
-        }
-    
-        if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
 
-            if (errno == EAGAIN)
-                break;
-            
-            debug(__FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
-            return -1;
+            if (pa_stream_write(i->play_stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                return -1;
+            }
+
+            i->buf = NULL;
+
+            assert(n >= (size_t) r);
+            n -= r;
         }
-    
-        if (pa_stream_write(i->stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
-            debug(__FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
+
+        if (n >= i->fragment_size)
+            i->io_flags |= PA_IO_EVENT_INPUT;
+        else
+            i->io_flags &= ~PA_IO_EVENT_INPUT;
+    }
+
+    if ((i->rec_stream) && (pa_stream_get_state(i->rec_stream) == PA_STREAM_READY)) {
+        n = pa_stream_readable_size(i->rec_stream);
+
+        if (n == (size_t)-1) {
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
+                pa_strerror(pa_context_errno(i->context)));
             return -1;
         }
 
-        i->buf = NULL;
+        while (n >= i->fragment_size || force) {
+            ssize_t r;
+            const void *data;
+            const char *buf;
+            size_t len;
+
+            if (pa_stream_peek(i->rec_stream, &data, &len) < 0) {
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                return -1;
+            }
 
-        assert(n >= (size_t) r);
-        n -= r;
+            if (!data)
+                break;
+
+            buf = (const char*)data + i->rec_offset;
+
+            if ((r = write(i->thread_fd, buf, len - i->rec_offset)) <= 0) {
+
+                if (errno == EAGAIN)
+                    break;
+
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": write(): %s\n", strerror(errno));
+                return -1;
+            }
+
+            assert((size_t)r <= len - i->rec_offset);
+            i->rec_offset += r;
+
+            if (i->rec_offset == len) {
+                if (pa_stream_drop(i->rec_stream) < 0) {
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                    return -1;
+                }
+                i->rec_offset = 0;
+            }
+
+            assert(n >= (size_t) r);
+            n -= r;
+        }
+
+        if (n >= i->fragment_size)
+            i->io_flags |= PA_IO_EVENT_OUTPUT;
+        else
+            i->io_flags &= ~PA_IO_EVENT_OUTPUT;
     }
 
     if (i->io_event) {
         pa_mainloop_api *api;
+
         api = pa_threaded_mainloop_get_api(i->mainloop);
-        api->io_enable(i->io_event, n >= i->fragment_size ? PA_IO_EVENT_INPUT : 0);
+        api->io_enable(i->io_event, i->io_flags);
     }
 
     return 0;
@@ -613,11 +897,11 @@ static void stream_state_cb(pa_stream *s, void * userdata) {
     switch (pa_stream_get_state(s)) {
 
         case PA_STREAM_READY:
-            debug(__FILE__": stream established.\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
             break;
             
         case PA_STREAM_FAILED:
-            debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
             fd_info_shutdown(i);
             break;
 
@@ -628,7 +912,7 @@ static void stream_state_cb(pa_stream *s, void * userdata) {
     }
 }
 
-static int create_stream(fd_info *i) {
+static int create_playback_stream(fd_info *i) {
     pa_buffer_attr attr;
     int n;
     
@@ -636,14 +920,14 @@ static int create_stream(fd_info *i) {
 
     fix_metrics(i);
 
-    if (!(i->stream = pa_stream_new(i->context, "Audio Stream", &i->sample_spec, NULL))) {
-        debug(__FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+    if (!(i->play_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
-    pa_stream_set_state_callback(i->stream, stream_state_cb, i);
-    pa_stream_set_write_callback(i->stream, stream_request_cb, i);
-    pa_stream_set_latency_update_callback(i->stream, stream_latency_update_cb, i);
+    pa_stream_set_state_callback(i->play_stream, stream_state_cb, i);
+    pa_stream_set_write_callback(i->play_stream, stream_request_cb, i);
+    pa_stream_set_latency_update_callback(i->play_stream, stream_latency_update_cb, i);
 
     memset(&attr, 0, sizeof(attr));
     attr.maxlength = i->fragment_size * (i->n_fragments+1);
@@ -651,8 +935,8 @@ static int create_stream(fd_info *i) {
     attr.prebuf = i->fragment_size;
     attr.minreq = i->fragment_size;
     
-    if (pa_stream_connect_playback(i->stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
-        debug(__FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+    if (pa_stream_connect_playback(i->play_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
@@ -667,13 +951,56 @@ fail:
     return -1;
 }
 
-static void free_stream(fd_info *i) {
+static int create_record_stream(fd_info *i) {
+    pa_buffer_attr attr;
+    int n;
+    
     assert(i);
 
-    if (i->stream) {
-        pa_stream_disconnect(i->stream);
-        pa_stream_unref(i->stream);
-        i->stream = NULL;
+    fix_metrics(i);
+
+    if (!(i->rec_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+        goto fail;
+    }
+
+    pa_stream_set_state_callback(i->rec_stream, stream_state_cb, i);
+    pa_stream_set_read_callback(i->rec_stream, stream_request_cb, i);
+    pa_stream_set_latency_update_callback(i->rec_stream, stream_latency_update_cb, i);
+
+    memset(&attr, 0, sizeof(attr));
+    attr.maxlength = i->fragment_size * (i->n_fragments+1);
+    attr.fragsize = i->fragment_size;
+    
+    if (pa_stream_connect_record(i->rec_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) < 0) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
+        goto fail;
+    }
+
+    n = i->fragment_size;
+    setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
+    n = i->fragment_size;
+    setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
+    
+    return 0;
+
+fail:
+    return -1;
+}
+
+static void free_streams(fd_info *i) {
+    assert(i);
+
+    if (i->play_stream) {
+        pa_stream_disconnect(i->play_stream);
+        pa_stream_unref(i->play_stream);
+        i->play_stream = NULL;
+    }
+
+    if (i->rec_stream) {
+        pa_stream_disconnect(i->rec_stream);
+        pa_stream_unref(i->rec_stream);
+        i->rec_stream = NULL;
     }
 }
 
@@ -684,17 +1011,24 @@ static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_even
     
     if (flags & PA_IO_EVENT_INPUT) {
 
-        if (!i->stream) {
-            api->io_enable(e, 0);
-
-            if (create_stream(i) < 0)
+        if (!i->play_stream) {
+            if (create_playback_stream(i) < 0)
                 goto fail;
-
         } else {
             if (fd_info_copy_data(i, 0) < 0)
                 goto fail;
         }
         
+    } else if (flags & PA_IO_EVENT_OUTPUT) {
+
+        if (!i->rec_stream) {
+            if (create_record_stream(i) < 0)
+                goto fail;
+        } else {
+            if (fd_info_copy_data(i, 0) < 0)
+                goto fail;
+        }
+
     } else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
         goto fail;
 
@@ -711,16 +1045,10 @@ static int dsp_open(int flags, int *_errno) {
     int ret;
     int f;
 
-    if ((flags != O_WRONLY) && (flags != (O_WRONLY|O_NONBLOCK))) {
-        *_errno = EACCES;
-        return -1;
-    }
-    
-    if (!(i = fd_info_new(FD_INFO_PLAYBACK, _errno)))
-        return -1;
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
 
-    shutdown(i->thread_fd, SHUT_WR);
-    shutdown(i->app_fd, SHUT_RD);
+    if (!(i = fd_info_new(FD_INFO_STREAM, _errno)))
+        return -1;
 
     if ((flags & O_NONBLOCK) == O_NONBLOCK) {
         if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
@@ -734,12 +1062,31 @@ static int dsp_open(int flags, int *_errno) {
 
     pa_threaded_mainloop_lock(i->mainloop);
     api = pa_threaded_mainloop_get_api(i->mainloop);
-    if (!(i->io_event = api->io_new(api, i->thread_fd, PA_IO_EVENT_INPUT, io_event_cb, i)))
+
+    switch (flags & O_ACCMODE) {
+    case O_RDONLY:
+        i->io_flags = PA_IO_EVENT_OUTPUT;
+        shutdown(i->thread_fd, SHUT_RD);
+        shutdown(i->app_fd, SHUT_WR);
+        break;
+    case O_WRONLY:
+        i->io_flags = PA_IO_EVENT_INPUT;
+        shutdown(i->thread_fd, SHUT_WR);
+        shutdown(i->app_fd, SHUT_RD);
+        break;
+    case O_RDWR:
+        i->io_flags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
+        break;
+    default:
+        return -1;
+    }
+
+    if (!(i->io_event = api->io_new(api, i->thread_fd, i->io_flags, io_event_cb, i)))
         goto fail;
     
     pa_threaded_mainloop_unlock(i->mainloop);
 
-    debug(__FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
 
     fd_info_add_to_list(i);
     ret = i->app_fd;
@@ -755,36 +1102,194 @@ fail:
     
     *_errno = EIO;
 
-    debug(__FILE__": dsp_open() failed\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
 
     return -1;
 }
 
+static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
+    fd_info *i = userdata;
+
+    if (!si && eol < 0) {
+        i->operation_success = 0;
+        pa_threaded_mainloop_signal(i->mainloop, 0);
+        return;
+    }
+
+    if (eol)
+        return;
+
+    if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
+        i->volume_modify_count++;
+    
+    i->sink_volume = si->volume;
+    i->sink_index = si->index;
+
+    i->operation_success = 1;
+    pa_threaded_mainloop_signal(i->mainloop, 0);
+}
+
+static void source_info_cb(pa_context *context, const pa_source_info *si, int eol, void *userdata) {
+    fd_info *i = userdata;
+
+    if (!si && eol < 0) {
+        i->operation_success = 0;
+        pa_threaded_mainloop_signal(i->mainloop, 0);
+        return;
+    }
+
+    if (eol)
+        return;
+
+    if (!pa_cvolume_equal(&i->source_volume, &si->volume))
+        i->volume_modify_count++;
+    
+    i->source_volume = si->volume;
+    i->source_index = si->index;
+
+    i->operation_success = 1;
+    pa_threaded_mainloop_signal(i->mainloop, 0);
+}
+
+static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+    fd_info *i = userdata;
+    pa_operation *o = NULL;
+
+    if (i->sink_index != idx)
+        return;
+
+    if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
+        return;
+
+    if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
+        return;
+    }
+
+    pa_operation_unref(o);
+}
+
 static int mixer_open(int flags, int *_errno) {
-/*     fd_info *i; */
+    fd_info *i;
+    pa_operation *o = NULL;
+    int ret;
 
-    *_errno = ENOSYS;
-    return -1;
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
+
+    if (!(i = fd_info_new(FD_INFO_MIXER, _errno))) 
+        return -1;
+    
+    pa_threaded_mainloop_lock(i->mainloop);
 
-/*     if (!(i = fd_info_new(FD_INFO_MIXER))) */
-/*         return -1; */
+    pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
+    
+    if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
 
+    i->operation_success = 0;
+    while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
+        pa_threaded_mainloop_wait(i->mainloop);
+        CONTEXT_CHECK_DEAD_GOTO(i, fail);
+    }
+
+    pa_operation_unref(o);
+    o = NULL;
+
+    if (!i->operation_success) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
+
+    /* Get sink info */
+
+    if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
+
+    i->operation_success = 0;
+    while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
+        pa_threaded_mainloop_wait(i->mainloop);
+        CONTEXT_CHECK_DEAD_GOTO(i, fail);
+    }
+
+    pa_operation_unref(o);
+    o = NULL;
+
+    if (!i->operation_success) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
+
+    /* Get source info */
+
+    if (!(o = pa_context_get_source_info_by_name(i->context, NULL, source_info_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
+
+    i->operation_success = 0;
+    while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
+        pa_threaded_mainloop_wait(i->mainloop);
+        CONTEXT_CHECK_DEAD_GOTO(i, fail);
+    }
+
+    pa_operation_unref(o);
+    o = NULL;
+
+    if (!i->operation_success) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
+        *_errno = EIO;
+        goto fail;
+    }
+
+    pa_threaded_mainloop_unlock(i->mainloop);
+
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
+
+    fd_info_add_to_list(i);
+    ret = i->app_fd;
+    fd_info_unref(i);
+    
+    return ret;
+
+fail:
+    if (o)
+        pa_operation_unref(o);
+
+    pa_threaded_mainloop_unlock(i->mainloop);
+
+    if (i)
+        fd_info_unref(i);
+    
+    *_errno = EIO;
+
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
+
+    return -1;
 }
 
 static int sndstat_open(int flags, int *_errno) {
     static const char sndstat[] =
-        "Sound Driver:3.8.1a-980706 (Polypaudio Virtual OSS)\n"
+        "Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
         "Kernel: POSIX\n"
         "Config options: 0\n"
         "\n"
         "Installed drivers:\n"
-        "Type 255: Polypaudio Virtual OSS\n"
+        "Type 255: PulseAudio Virtual OSS\n"
         "\n"
         "Card config:\n"
-        "Polypaudio Virtual OSS\n"
+        "PulseAudio Virtual OSS\n"
         "\n"
         "Audio devices:\n"
-        "0: Polypaudio Virtual OSS\n"
+        "0: PulseAudio Virtual OSS\n"
         "\n"
         "Synth devices: NOT ENABLED IN CONFIG\n"
         "\n"
@@ -792,19 +1297,23 @@ static int sndstat_open(int flags, int *_errno) {
         "\n"
         "Timers:\n"
         "\n"
-        "\Mixers:\n"
-        "0: Polypaudio Virtual OSS\n";
+        "Mixers:\n"
+        "0: PulseAudio Virtual OSS\n";
 
     char fn[] = "/tmp/padsp-sndstat-XXXXXX";
     mode_t u;
     int fd = -1;
     int e;
 
-    debug(__FILE__": sndstat_open()\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
     
-    if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
+    if (flags != O_RDONLY
+#ifdef O_LARGEFILE
+       && flags != (O_RDONLY|O_LARGEFILE)
+#endif
+       ) {
         *_errno = EACCES;
-        debug(__FILE__": bad access!\n");
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
         goto fail;
     }
 
@@ -815,7 +1324,7 @@ static int sndstat_open(int flags, int *_errno) {
 
     if (fd < 0) {
         *_errno = e;
-        debug(__FILE__": mkstemp() failed: %s\n", strerror(errno));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
         goto fail;
     }
 
@@ -823,13 +1332,13 @@ static int sndstat_open(int flags, int *_errno) {
 
     if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
         *_errno = errno;
-        debug(__FILE__": write() failed: %s\n", strerror(errno));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
         goto fail;
     }
 
     if (lseek(fd, SEEK_SET, 0) < 0) {
         *_errno = errno;
-        debug(__FILE__": lseek() failed: %s\n", strerror(errno));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
         goto fail;
     }
 
@@ -846,11 +1355,15 @@ int open(const char *filename, int flags, ...) {
     mode_t mode = 0;
     int r, _errno = 0;
 
-    debug(__FILE__": open(%s)\n", filename);
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
 
     va_start(args, flags);
-    if (flags & O_CREAT)
+    if (flags & O_CREAT) {
+      if (sizeof(mode_t) < sizeof(int))
+       mode = va_arg(args, int);
+      else
         mode = va_arg(args, mode_t);
+    }
     va_end(args);
 
     if (!function_enter()) {
@@ -858,11 +1371,11 @@ int open(const char *filename, int flags, ...) {
         return _open(filename, flags, mode);
     }
 
-    if (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0) {
+    if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
         r = dsp_open(flags, &_errno);
-    } else if (strcmp(filename, "/dev/mixer") == 0) {
+    } else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
         r = mixer_open(flags, &_errno);
-    } else if (strcmp(filename, "/dev/sndstat") == 0) {
+    } else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
         r = sndstat_open(flags, &_errno);
     } else {
         function_exit();
@@ -879,8 +1392,158 @@ int open(const char *filename, int flags, ...) {
 }
 
 static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
-    *_errno = ENOSYS;
-    return -1;
+    int ret = -1;
+    
+    switch (request) {
+        case SOUND_MIXER_READ_DEVMASK :
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
+
+            *(int*) argp = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
+            break;
+
+        case SOUND_MIXER_READ_RECMASK :
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
+
+            *(int*) argp = SOUND_MASK_IGAIN;
+            break;
+            
+        case SOUND_MIXER_READ_STEREODEVS:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+            *(int*) argp = 0;
+            if (i->sink_volume.channels > 1)
+                *(int*) argp |= SOUND_MASK_PCM;
+            if (i->source_volume.channels > 1)
+                *(int*) argp |= SOUND_MASK_IGAIN;
+            pa_threaded_mainloop_unlock(i->mainloop);
+            
+            break;
+
+        case SOUND_MIXER_READ_RECSRC:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
+
+            *(int*) argp = SOUND_MASK_IGAIN;
+            break;
+
+        case SOUND_MIXER_WRITE_RECSRC:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_RECSRC\n");
+            break;
+
+        case SOUND_MIXER_READ_CAPS:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
+
+            *(int*) argp = 0;
+            break;
+    
+        case SOUND_MIXER_READ_PCM:
+        case SOUND_MIXER_READ_IGAIN: {
+            pa_cvolume *v;
+
+            if (request == SOUND_MIXER_READ_PCM)
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
+            else
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_IGAIN\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+
+            if (request == SOUND_MIXER_READ_PCM)
+                v = &i->sink_volume;
+            else
+                v = &i->source_volume;
+
+            *(int*) argp =
+                ((v->values[0]*100/PA_VOLUME_NORM)) |
+                ((v->values[v->channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM)  << 8);
+
+            pa_threaded_mainloop_unlock(i->mainloop);
+
+            break;
+        }
+
+        case SOUND_MIXER_WRITE_PCM:
+        case SOUND_MIXER_WRITE_IGAIN: {
+            pa_cvolume v, *pv;
+
+            if (request == SOUND_MIXER_READ_PCM)
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
+            else
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
+
+            pa_threaded_mainloop_lock(i->mainloop);
+
+            if (request == SOUND_MIXER_READ_PCM) {
+                v = i->sink_volume;
+                pv = &i->sink_volume;
+            } else {
+                v = i->source_volume;
+                pv = &i->source_volume;
+            }
+
+            pv->values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
+            pv->values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
+
+            if (!pa_cvolume_equal(pv, &v)) {
+                pa_operation *o;
+
+                if (request == SOUND_MIXER_READ_PCM)
+                    o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, context_success_cb, i);
+                else
+                    o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, context_success_cb, i);
+
+                if (!o)
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
+                else {
+
+                    i->operation_success = 0;
+                    while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
+                        CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
+                        
+                        pa_threaded_mainloop_wait(i->mainloop);
+                    }
+                exit_loop:
+                    
+                    if (!i->operation_success)
+                        debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
+
+                    pa_operation_unref(o);
+                }
+                
+                /* We don't wait for completion here */
+                i->volume_modify_count++;
+            }
+            
+            pa_threaded_mainloop_unlock(i->mainloop);
+            
+            break;
+        }
+
+        case SOUND_MIXER_INFO: {
+            mixer_info *mi = argp;
+
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
+
+            memset(mi, 0, sizeof(mixer_info));
+            strncpy(mi->id, "PULSEAUDIO", sizeof(mi->id));
+            strncpy(mi->name, "PulseAudio Virtual OSS", sizeof(mi->name));
+            pa_threaded_mainloop_lock(i->mainloop);
+            mi->modify_counter = i->volume_modify_count;
+            pa_threaded_mainloop_unlock(i->mainloop);
+            break;
+        }
+            
+        default:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
+
+            *_errno = EINVAL;
+            goto fail;
+    }
+
+    ret = 0;
+    
+fail:
+    
+    return ret;
 }
 
 static int map_format(int *fmt, pa_sample_spec *ss) {
@@ -936,24 +1599,12 @@ static int map_format_back(pa_sample_format_t format) {
     }
 }
 
-static void success_cb(pa_stream *s, int success, void *userdata) {
-    fd_info *i = userdata;
-
-    assert(s);
-    assert(i);
-
-    i->operation_success = success;
-    pa_threaded_mainloop_signal(i->mainloop, 0);
-}
-
-static int dsp_flush_socket(fd_info *i) {
+static int dsp_flush_fd(int fd) {
+#ifdef SIOCINQ
     int l;
-        
-    if (i->thread_fd < 0)
-        return -1;
 
-    if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
-        debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
+    if (ioctl(fd, SIOCINQ, &l) < 0) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
         return -1;
     }
 
@@ -962,36 +1613,68 @@ static int dsp_flush_socket(fd_info *i) {
         size_t k;
 
         k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
-        if (read(i->thread_fd, buf, k) < 0)
-            debug(__FILE__": read(): %s\n", strerror(errno));
+        if (read(fd, buf, k) < 0)
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
         l -= k;
     }
 
+    return 0;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+    return 0;
+#endif
+}
+
+static int dsp_flush_socket(fd_info *i) {
+    int res = 0;
+
+    if ((i->thread_fd < 0) && (i->app_fd < 0))
+        return -1;
+
+    if (i->thread_fd >= 0)
+        res = dsp_flush_fd(i->thread_fd);
+
+    if (res < 0)
+        return res;
+
+    if (i->app_fd >= 0)
+        res = dsp_flush_fd(i->app_fd);
+
+    if (res < 0)
+        return res;
+
     return 0;
 }
 
 static int dsp_empty_socket(fd_info *i) {
+#ifdef SIOCINQ
     int ret = -1;
     
     /* Empty the socket */
     for (;;) {
         int l;
         
-        if (i->thread_fd < 0 || !i->stream)
+        if (i->thread_fd < 0)
             break;
         
         if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
-            debug(__FILE__": SIOCINQ: %s\n", strerror(errno));
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
             break;
         }
 
-        if (!l)
+        if (!l) {
+            ret = 0;
             break;
+        }
         
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
     return ret;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+    return 0;
+#endif
 }
 
 static int dsp_drain(fd_info *i) {
@@ -1001,33 +1684,32 @@ static int dsp_drain(fd_info *i) {
     if (!i->mainloop)
         return 0;
     
-    debug(__FILE__": Draining.\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
 
     pa_threaded_mainloop_lock(i->mainloop);
 
     if (dsp_empty_socket(i) < 0)
         goto fail;
     
-    if (!i->stream)
+    if (!i->play_stream)
         goto fail;
 
-    debug(__FILE__": Really draining.\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
         
-    if (!(o = pa_stream_drain(i->stream, success_cb, i))) {
-        debug(__FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
+    if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
     i->operation_success = 0;
     while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
-        if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
-            goto fail;
+        PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
             
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
     if (!i->operation_success) {
-        debug(__FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
@@ -1047,7 +1729,7 @@ static int dsp_trigger(fd_info *i) {
     pa_operation *o = NULL;
     int r = -1;
 
-    if (!i->stream)
+    if (!i->play_stream)
         return 0;
 
     pa_threaded_mainloop_lock(i->mainloop);
@@ -1055,23 +1737,22 @@ static int dsp_trigger(fd_info *i) {
     if (dsp_empty_socket(i) < 0)
         goto fail;
 
-    debug(__FILE__": Triggering.\n");
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
         
-    if (!(o = pa_stream_trigger(i->stream, success_cb, i))) {
-        debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
+    if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
     i->operation_success = 0;
     while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
-        if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
-            goto fail;
+        PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
             
         pa_threaded_mainloop_wait(i->mainloop);
     }
 
     if (!i->operation_success) {
-        debug(__FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
         goto fail;
     }
 
@@ -1092,7 +1773,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
     
     switch (request) {
         case SNDCTL_DSP_SETFMT: {
-            debug(__FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
             
             pa_threaded_mainloop_lock(i->mainloop);
 
@@ -1100,7 +1781,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
                 *(int*) argp = map_format_back(i->sample_spec.format);
             else {
                 map_format((int*) argp, &i->sample_spec);
-                free_stream(i);
+                free_streams(i);
             }
 
             pa_threaded_mainloop_unlock(i->mainloop);
@@ -1112,7 +1793,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
             int valid;
             char t[256];
             
-            debug(__FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
 
             pa_threaded_mainloop_lock(i->mainloop);
 
@@ -1121,10 +1802,10 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             if ((valid = pa_sample_spec_valid(&ss))) {
                 i->sample_spec = ss;
-                free_stream(i);
+                free_streams(i);
             }
             
-            debug(__FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
 
             pa_threaded_mainloop_unlock(i->mainloop);
 
@@ -1137,12 +1818,12 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
         }
             
         case SNDCTL_DSP_STEREO:
-            debug(__FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
             
             pa_threaded_mainloop_lock(i->mainloop);
             
             i->sample_spec.channels = *(int*) argp ? 2 : 1;
-            free_stream(i);
+            free_streams(i);
             
             pa_threaded_mainloop_unlock(i->mainloop);
             return 0;
@@ -1151,7 +1832,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
             pa_sample_spec ss;
             int valid;
             
-            debug(__FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
             
             pa_threaded_mainloop_lock(i->mainloop);
 
@@ -1160,7 +1841,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
 
             if ((valid = pa_sample_spec_valid(&ss))) {
                 i->sample_spec = ss;
-                free_stream(i);
+                free_streams(i);
             }
             
             pa_threaded_mainloop_unlock(i->mainloop);
@@ -1174,7 +1855,7 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
         }
 
         case SNDCTL_DSP_GETBLKSIZE:
-            debug(__FILE__": SNDCTL_DSP_GETBLKSIZE\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
 
             pa_threaded_mainloop_lock(i->mainloop);
 
@@ -1186,29 +1867,37 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
             break;
 
         case SNDCTL_DSP_SETFRAGMENT:
-            debug(__FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
             
             pa_threaded_mainloop_lock(i->mainloop);
             
             i->fragment_size = 1 << (*(int*) argp);
             i->n_fragments = (*(int*) argp) >> 16;
-            
-            free_stream(i);
+
+            /* 0x7FFF means that we can set whatever we like */
+            if (i->n_fragments == 0x7FFF)
+                i->n_fragments = 12;
+
+            free_streams(i);
             
             pa_threaded_mainloop_unlock(i->mainloop);
             
             break;
             
         case SNDCTL_DSP_GETCAPS:
-            debug(__FILE__": SNDCTL_DSP_CAPS\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
             
-            *(int*)  argp = DSP_CAP_MULTI;
+            *(int*)  argp = DSP_CAP_DUPLEX
+#ifdef DSP_CAP_MULTI
+             | DSP_CAP_MULTI
+#endif
+             ;
             break;
 
         case SNDCTL_DSP_GETODELAY: {
             int l;
             
-            debug(__FILE__": SNDCTL_DSP_GETODELAY\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
             
             pa_threaded_mainloop_lock(i->mainloop);
 
@@ -1216,40 +1905,46 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
             
             for (;;) {
                 pa_usec_t usec;
-                if (!i->stream || pa_stream_get_state(i->stream) != PA_STREAM_READY)
-                    break;
 
-                if (pa_stream_get_latency(i->stream, &usec, NULL) >= 0) {
+                PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
+
+                if (pa_stream_get_latency(i->play_stream, &usec, NULL) >= 0) {
                     *(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
                     break;
                 }
 
                 if (pa_context_errno(i->context) != PA_ERR_NODATA) {
-                    debug(__FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
                     break;
                 }
 
                 pa_threaded_mainloop_wait(i->mainloop);
             }
             
+        exit_loop:
+
+#ifdef SIOCINQ            
             if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
-                debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
             else
                 *(int*) argp += l;
+#else
+# warning "Your platform does not support SIOCINQ, something might not work as intended."
+#endif
 
             pa_threaded_mainloop_unlock(i->mainloop);
 
-            debug(__FILE__": ODELAY: %i\n", *(int*) argp);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
 
             break;
         }
             
         case SNDCTL_DSP_RESET: {
-            debug(__FILE__": SNDCTL_DSP_RESET\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
             
             pa_threaded_mainloop_lock(i->mainloop);
 
-            free_stream(i);
+            free_streams(i);
             dsp_flush_socket(i);
             reset_params(i);
             
@@ -1258,64 +1953,100 @@ static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno)
         }
             
         case SNDCTL_DSP_GETFMTS: {
-            debug(__FILE__": SNDCTL_DSP_GETFMTS\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
             
             *(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
             break;
         }
 
         case SNDCTL_DSP_POST:
-            debug(__FILE__": SNDCTL_DSP_POST\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
             
             if (dsp_trigger(i) < 0) 
                 *_errno = EIO;
             break;
 
         case SNDCTL_DSP_SYNC: 
-            debug(__FILE__": SNDCTL_DSP_SYNC\n");
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
             
             if (dsp_drain(i) < 0) 
                 *_errno = EIO;
 
             break;
 
-        case SNDCTL_DSP_GETOSPACE: {
+        case SNDCTL_DSP_GETOSPACE:
+        case SNDCTL_DSP_GETISPACE: {
             audio_buf_info *bi = (audio_buf_info*) argp;
-            int l;
+            int l = 0;
             size_t k = 0;
 
-            debug(__FILE__": SNDCTL_DSP_GETOSPACE\n");
+            if (request == SNDCTL_DSP_GETOSPACE)
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
+            else
+                debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETISPACE\n");
 
             pa_threaded_mainloop_lock(i->mainloop);
 
             fix_metrics(i);
-            
-            if (i->stream) {
-                if ((k = pa_stream_writable_size(i->stream)) == (size_t) -1)
-                    debug(__FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
-            } else
-                k = i->fragment_size * i->n_fragments;
-            
-            if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
-                debug(__FILE__": SIOCINQ failed: %s\n", strerror(errno));
-                l = 0;
+
+            if (request == SNDCTL_DSP_GETOSPACE) {
+                if (i->play_stream) {
+                    if ((k = pa_stream_writable_size(i->play_stream)) == (size_t) -1)
+                        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                } else
+                    k = i->fragment_size * i->n_fragments;
+
+#ifdef SIOCINQ
+                if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
+                    l = 0;
+                }
+#else
+# warning "Your platform does not dsp_flush_fd, something might not work as intended."
+#endif
+
+                bi->bytes = k > (size_t) l ? k - l : 0;
+            } else {
+                if (i->rec_stream) {
+                    if ((k = pa_stream_readable_size(i->rec_stream)) == (size_t) -1)
+                        debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
+                } else
+                    k = 0;
+
+#ifdef SIOCINQ
+                if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
+                    debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
+                    l = 0;
+                }
+#else
+# warning "Your platform does not dsp_flush_fd, something might not work as intended."
+#endif
+                bi->bytes = k + l;
             }
 
             bi->fragsize = i->fragment_size;
             bi->fragstotal = i->n_fragments;
-            bi->bytes = k > (size_t) l ? k - l : 0;
             bi->fragments = bi->bytes / bi->fragsize;
 
             pa_threaded_mainloop_unlock(i->mainloop);
 
-            debug(__FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
 
             break;
         }
-            
+
+        case SNDCTL_DSP_GETIPTR:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETIPTR\n");
+            goto inval;
+
+        case SNDCTL_DSP_GETOPTR:
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": invalid ioctl SNDCTL_DSP_GETOPTR\n");
+            goto inval;
+
         default:
-            debug(__FILE__": unknwon ioctl 0x%08lx\n", request);
+            debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
 
+inval:
             *_errno = EINVAL;
             goto fail;
     }
@@ -1333,7 +2064,7 @@ int ioctl(int fd, unsigned long request, ...) {
     void *argp;
     int r, _errno = 0;
 
-    debug(__FILE__": ioctl()\n");
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
 
     va_start(args, request);
     argp = va_arg(args, void *);
@@ -1368,7 +2099,7 @@ int ioctl(int fd, unsigned long request, ...) {
 int close(int fd) {
     fd_info *i;
 
-    debug(__FILE__": close()\n");
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
 
     if (!function_enter()) {
         LOAD_CLOSE_FUNC();
@@ -1389,11 +2120,35 @@ int close(int fd) {
     return 0;
 }
 
+int access(const char *pathname, int mode) {
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
+
+    if (strcmp(pathname, "/dev/dsp") != 0 &&
+        strcmp(pathname, "/dev/adsp") != 0 &&
+        strcmp(pathname, "/dev/sndstat") != 0 &&
+        strcmp(pathname, "/dev/mixer") != 0) {
+        LOAD_ACCESS_FUNC();
+        return _access(pathname, mode);
+    }
+
+    if (mode & (W_OK | X_OK)) {
+        debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
+        errno = EACCES;
+        return -1;
+    }
+
+    debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
+
+    return 0;
+}
+
+#ifdef HAVE_OPEN64
+
 int open64(const char *filename, int flags, ...) {
     va_list args;
     mode_t mode = 0;
 
-    debug(__FILE__": open64(%s)\n", filename);
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
     
     va_start(args, flags);
     if (flags & O_CREAT)
@@ -1411,36 +2166,38 @@ int open64(const char *filename, int flags, ...) {
     return open(filename, flags, mode);
 }
 
+#endif
+
 FILE* fopen(const char *filename, const char *mode) {
     FILE *f = NULL;
     int fd;
     mode_t m;
     
-    debug(__FILE__": fopen(%s)\n", filename);
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
 
-    if (strcmp(filename, "/dev/dsp") == 0 ||
-        strcmp(filename, "/dev/adsp") == 0) {
-
-        if (strcmp(mode, "wb") != 0) {
-            errno = EACCES;
-            return NULL;
-        }
+    if (strcmp(filename, "/dev/dsp") != 0 &&
+        strcmp(filename, "/dev/adsp") != 0 &&
+        strcmp(filename, "/dev/sndstat") != 0 &&
+        strcmp(filename, "/dev/mixer") != 0) {
+        LOAD_FOPEN_FUNC();
+        return _fopen(filename, mode);
+    }
 
+    switch (mode[0]) {
+    case 'r':
+        m = O_RDONLY;
+        break;
+    case 'w':
+    case 'a':
         m = O_WRONLY;
-    } else if (strcmp(filename, "/dev/sndstat") == 0) {
-
-        if (strcmp(mode, "r") != 0) {
-            errno = EACCES;
-            return NULL;
-        }
+        break;
+    default:
+        errno = EINVAL;
+        return NULL;
+    }
 
-        m = O_RDONLY;
-    } else if (strcmp(filename, "/dev/mixer") != 0)
+    if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
         m = O_RDWR;
-    else {
-        LOAD_FOPEN_FUNC();
-        return _fopen(filename, mode);
-    }
 
     if ((fd = open(filename, m)) < 0)
         return NULL;
@@ -1453,9 +2210,11 @@ FILE* fopen(const char *filename, const char *mode) {
     return f;
 }
 
+#ifdef HAVE_OPEN64
+
 FILE *fopen64(const char *filename, const char *mode) {
 
-    debug(__FILE__": fopen64(%s)\n", filename);
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
 
     if (strcmp(filename, "/dev/dsp") != 0 &&
         strcmp(filename, "/dev/adsp") != 0 &&
@@ -1468,10 +2227,12 @@ FILE *fopen64(const char *filename, const char *mode) {
     return fopen(filename, mode);
 }
 
+#endif
+
 int fclose(FILE *f) {
     fd_info *i;
 
-    debug(__FILE__": fclose()\n");
+    debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
 
     if (!function_enter()) {
         LOAD_FCLOSE_FUNC();