SDL: reverted jack audio backend removal.

From 016b75731192ada9209dac92e3bbfe9fd513693c Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Wed, 23 Nov 2022 06:50:20 +0300
Subject: [PATCH] reverted jack audio backend removal.

---
 .github/workflows/main.yml      |   2 +-
 .github/workflows/vmactions.yml |   2 +
 CMakeLists.txt                  |   3 +
 cmake/sdlchecks.cmake           |  29 +++
 configure                       | 139 ++++++++++
 configure.ac                    |  45 ++++
 docs/README-linux.md            |   4 +-
 include/SDL_config.h.cmake      |   2 +
 include/SDL_config.h.in         |   2 +
 src/audio/SDL_audio.c           |   3 +
 src/audio/SDL_sysaudio.h        |   1 +
 src/audio/jack/SDL_jackaudio.c  | 446 ++++++++++++++++++++++++++++++++
 src/audio/jack/SDL_jackaudio.h  |  41 +++
 13 files changed, 716 insertions(+), 3 deletions(-)
 create mode 100644 src/audio/jack/SDL_jackaudio.c
 create mode 100644 src/audio/jack/SDL_jackaudio.h

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 43309172a953..250f8905ce53 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -45,7 +45,7 @@ jobs:
         sudo apt-get update
         sudo apt-get install build-essential git make autoconf automake libtool \
             pkg-config cmake ninja-build gnome-desktop-testing libasound2-dev libpulse-dev \
-            libsndio-dev libsamplerate0-dev libx11-dev libxext-dev \
+            libaudio-dev libjack-dev libsndio-dev libsamplerate0-dev libx11-dev libxext-dev \
             libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libwayland-dev \
             libxkbcommon-dev libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev \
             libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev fcitx-libs-dev
diff --git a/.github/workflows/vmactions.yml b/.github/workflows/vmactions.yml
index b6cc311a22b8..9d6fdffe83d2 100644
--- a/.github/workflows/vmactions.yml
+++ b/.github/workflows/vmactions.yml
@@ -32,8 +32,10 @@ jobs:
               evdev-proto \
               libinotify \
               alsa-lib \
+              jackit \
               pipewire \
               pulseaudio \
+              sndio \
               dbus \
               zh-fcitx \
               ibus \
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 130cfa7fab83..5817c1b9f0e5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -452,6 +452,8 @@ dep_option(SDL_PTHREADS_SEM        "Use pthread semaphores" ON "SDL_PTHREADS" OF
 dep_option(SDL_OSS                 "Support the OSS audio API" ON "UNIX_SYS OR RISCOS" OFF)
 set_option(SDL_ALSA                "Support the ALSA audio API" ${UNIX_SYS})
 dep_option(SDL_ALSA_SHARED         "Dynamically load ALSA audio support" ON "SDL_ALSA" OFF)
+set_option(SDL_JACK                "Support the JACK audio API" ${UNIX_SYS})
+dep_option(SDL_JACK_SHARED         "Dynamically load JACK audio support" ON "SDL_JACK" OFF)
 set_option(SDL_PIPEWIRE            "Use Pipewire audio" ${UNIX_SYS})
 dep_option(SDL_PIPEWIRE_SHARED     "Dynamically load Pipewire support" ON "SDL_PIPEWIRE" OFF)
 set_option(SDL_PULSEAUDIO          "Use PulseAudio" ${UNIX_SYS})
@@ -1393,6 +1395,7 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
     endif()
     CheckOSS()
     CheckALSA()
+    CheckJACK()
     CheckPipewire()
     CheckPulseAudio()
     CheckSNDIO()
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 750d55f00fa7..c6a679f7eddd 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -194,6 +194,35 @@ macro(CheckSNDIO)
   endif()
 endmacro()
 
+# Requires:
+# - PkgCheckModules
+# Optional:
+# - SDL_JACK_SHARED opt
+# - HAVE_SDL_LOADSO opt
+macro(CheckJACK)
+  if(SDL_JACK)
+    pkg_check_modules(PKG_JACK jack)
+    if(PKG_JACK_FOUND)
+      set(HAVE_JACK TRUE)
+      file(GLOB JACK_SOURCES ${SDL3_SOURCE_DIR}/src/audio/jack/*.c)
+      list(APPEND SOURCE_FILES ${JACK_SOURCES})
+      set(SDL_AUDIO_DRIVER_JACK 1)
+      list(APPEND EXTRA_CFLAGS ${PKG_JACK_CFLAGS})
+      if(SDL_JACK_SHARED AND NOT HAVE_SDL_LOADSO)
+        message_warn("You must have SDL_LoadObject() support for dynamic JACK audio loading")
+      endif()
+      FindLibraryAndSONAME("jack" LIBDIRS ${PKG_JACK_LIBRARY_DIRS})
+      if(SDL_JACK_SHARED AND JACK_LIB AND HAVE_SDL_LOADSO)
+        set(SDL_AUDIO_DRIVER_JACK_DYNAMIC "\"${JACK_LIB_SONAME}\"")
+        set(HAVE_JACK_SHARED TRUE)
+      else()
+        list(APPEND EXTRA_LDFLAGS ${PKG_JACK_LDFLAGS})
+      endif()
+      set(HAVE_SDL_AUDIO TRUE)
+    endif()
+  endif()
+endmacro()
+
 # Requires:
 # - SDL_LIBSAMPLERATE
 # Optional:
diff --git a/configure b/configure
index 843e75bd389b..b1cc122569ff 100755
--- a/configure
+++ b/configure
@@ -710,6 +710,8 @@ PULSEAUDIO_LIBS
 PULSEAUDIO_CFLAGS
 PIPEWIRE_LIBS
 PIPEWIRE_CFLAGS
+JACK_LIBS
+JACK_CFLAGS
 ALSA_LIBS
 ALSA_CFLAGS
 ALLOCA
@@ -868,6 +870,8 @@ with_alsa_prefix
 with_alsa_inc_prefix
 enable_alsatest
 enable_alsa_shared
+enable_jack
+enable_jack_shared
 enable_pipewire
 enable_pipewire_shared
 enable_pulseaudio
@@ -948,6 +952,8 @@ PKG_CONFIG
 PKG_CONFIG_PATH
 PKG_CONFIG_LIBDIR
 CPP
+JACK_CFLAGS
+JACK_LIBS
 PIPEWIRE_CFLAGS
 PIPEWIRE_LIBS
 PULSEAUDIO_CFLAGS
@@ -1642,6 +1648,8 @@ Optional Features:
   --enable-alsa           support the ALSA audio API [default=yes]
   --disable-alsatest      Do not try to compile and run a test Alsa program
   --enable-alsa-shared    dynamically load ALSA audio support [default=yes]
+  --enable-jack           use JACK audio [default=yes]
+  --enable-jack-shared    dynamically load JACK audio support [default=yes]
   --enable-pipewire       use Pipewire audio [default=yes]
   --enable-pipewire-shared
                           dynamically load Pipewire support [default=yes]
@@ -1767,6 +1775,8 @@ Some influential environment variables:
   PKG_CONFIG_LIBDIR
               path overriding pkg-config's built-in search path
   CPP         C preprocessor
+  JACK_CFLAGS C compiler flags for JACK, overriding pkg-config
+  JACK_LIBS   linker flags for JACK, overriding pkg-config
   PIPEWIRE_CFLAGS
               C compiler flags for PIPEWIRE, overriding pkg-config
   PIPEWIRE_LIBS
@@ -21252,6 +21262,134 @@ printf "%s\n" "#define SDL_AUDIO_DRIVER_ALSA_DYNAMIC \"$alsa_lib\"" >>confdefs.h
     fi
 }
 
+CheckJACK()
+{
+    # Check whether --enable-jack was given.
+if test ${enable_jack+y}
+then :
+  enableval=$enable_jack;
+else $as_nop
+  enable_jack=yes
+fi
+
+    if test x$enable_audio = xyes -a x$enable_jack = xyes; then
+
+pkg_failed=no
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for jack >= 0.125" >&5
+printf %s "checking for jack >= 0.125... " >&6; }
+
+if test -n "$JACK_CFLAGS"; then
+    pkg_cv_JACK_CFLAGS="$JACK_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"jack >= 0.125\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "jack >= 0.125") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_JACK_CFLAGS=`$PKG_CONFIG --cflags "jack >= 0.125" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$JACK_LIBS"; then
+    pkg_cv_JACK_LIBS="$JACK_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"jack >= 0.125\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "jack >= 0.125") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_JACK_LIBS=`$PKG_CONFIG --libs "jack >= 0.125" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi
+        if test $_pkg_short_errors_supported = yes; then
+	        JACK_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "jack >= 0.125" 2>&1`
+        else
+	        JACK_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "jack >= 0.125" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$JACK_PKG_ERRORS" >&5
+
+	audio_jack=no
+elif test $pkg_failed = untried; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+	audio_jack=no
+else
+	JACK_CFLAGS=$pkg_cv_JACK_CFLAGS
+	JACK_LIBS=$pkg_cv_JACK_LIBS
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+	audio_jack=yes
+fi
+
+        if test x$audio_jack = xyes; then
+            # Check whether --enable-jack-shared was given.
+if test ${enable_jack_shared+y}
+then :
+  enableval=$enable_jack_shared;
+else $as_nop
+  enable_jack_shared=yes
+fi
+
+            jack_lib=`find_lib "libjack.so.*" "$JACK_LIBS" | sed 's/.*\/\(.*\)/\1/; q'`
+
+
+printf "%s\n" "#define SDL_AUDIO_DRIVER_JACK 1" >>confdefs.h
+
+            SOURCES="$SOURCES $srcdir/src/audio/jack/*.c"
+            EXTRA_CFLAGS="$EXTRA_CFLAGS $JACK_CFLAGS"
+            if test x$have_loadso != xyes && \
+               test x$enable_jack_shared = xyes; then
+                { printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: You must have SDL_LoadObject() support for dynamic JACK audio loading" >&5
+printf "%s\n" "$as_me: WARNING: You must have SDL_LoadObject() support for dynamic JACK audio loading" >&2;}
+            fi
+            if test x$have_loadso = xyes && \
+               test x$enable_jack_shared = xyes && test x$jack_lib != x; then
+                echo "-- dynamic libjack -> $jack_lib"
+
+printf "%s\n" "#define SDL_AUDIO_DRIVER_JACK_DYNAMIC \"$jack_lib\"" >>confdefs.h
+
+                SUMMARY_audio="${SUMMARY_audio} jack(dynamic)"
+
+                case "$host" in
+                    # On Solaris, jack must be linked deferred explicitly
+                    # to prevent undefined symbol failures.
+                    *-*-solaris*)
+                        JACK_LIBS=`echo $JACK_LIBS | sed 's/\-l/-Wl,-l/g'`
+                        EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-zdeferred $JACK_LIBS -Wl,-znodeferred"
+                esac
+            else
+                EXTRA_LDFLAGS="$EXTRA_LDFLAGS $JACK_LIBS"
+                SUMMARY_audio="${SUMMARY_audio} jack"
+            fi
+            have_audio=yes
+        fi
+    fi
+}
+
 CheckPipewire()
 {
     # Check whether --enable-pipewire was given.
@@ -27145,6 +27283,7 @@ printf "%s\n" "#define SDL_VIDEO_DRIVER_ANDROID 1" >>confdefs.h
         CheckALSA
         CheckPipewire
         CheckPulseAudio
+        CheckJACK
         CheckSNDIO
         CheckLibSampleRate
         # Need to check for Raspberry PI first and add platform specific compiler flags, otherwise the test for GLES fails!
diff --git a/configure.ac b/configure.ac
index aec3c4fb8a51..df299309998d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -988,6 +988,50 @@ CheckALSA()
     fi
 }
 
+dnl Find JACK Audio
+CheckJACK()
+{
+    AC_ARG_ENABLE(jack,
+[AS_HELP_STRING([--enable-jack], [use JACK audio [default=yes]])],
+                  , enable_jack=yes)
+    if test x$enable_audio = xyes -a x$enable_jack = xyes; then
+        PKG_CHECK_MODULES([JACK], [jack >= 0.125], audio_jack=yes, audio_jack=no)
+
+        if test x$audio_jack = xyes; then
+            AC_ARG_ENABLE(jack-shared,
+[AS_HELP_STRING([--enable-jack-shared], [dynamically load JACK audio support [default=yes]])],
+                          , enable_jack_shared=yes)
+            jack_lib=[`find_lib "libjack.so.*" "$JACK_LIBS" | sed 's/.*\/\(.*\)/\1/; q'`]
+
+            AC_DEFINE(SDL_AUDIO_DRIVER_JACK, 1, [ ])
+            SOURCES="$SOURCES $srcdir/src/audio/jack/*.c"
+            EXTRA_CFLAGS="$EXTRA_CFLAGS $JACK_CFLAGS"
+            if test x$have_loadso != xyes && \
+               test x$enable_jack_shared = xyes; then
+                AC_MSG_WARN([You must have SDL_LoadObject() support for dynamic JACK audio loading])
+            fi
+            if test x$have_loadso = xyes && \
+               test x$enable_jack_shared = xyes && test x$jack_lib != x; then
+                echo "-- dynamic libjack -> $jack_lib"
+                AC_DEFINE_UNQUOTED(SDL_AUDIO_DRIVER_JACK_DYNAMIC, "$jack_lib", [ ])
+                SUMMARY_audio="${SUMMARY_audio} jack(dynamic)"
+
+                case "$host" in
+                    # On Solaris, jack must be linked deferred explicitly
+                    # to prevent undefined symbol failures.
+                    *-*-solaris*)
+                        JACK_LIBS=`echo $JACK_LIBS | sed 's/\-l/-Wl,-l/g'`
+                        EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-zdeferred $JACK_LIBS -Wl,-znodeferred"
+                esac
+            else
+                EXTRA_LDFLAGS="$EXTRA_LDFLAGS $JACK_LIBS"
+                SUMMARY_audio="${SUMMARY_audio} jack"
+            fi
+            have_audio=yes
+        fi
+    fi
+}
+
 dnl Find Pipewire
 CheckPipewire()
 {
@@ -3419,6 +3463,7 @@ case "$host" in
         CheckALSA
         CheckPipewire
         CheckPulseAudio
+        CheckJACK
         CheckSNDIO
         CheckLibSampleRate
         # Need to check for Raspberry PI first and add platform specific compiler flags, otherwise the test for GLES fails!
diff --git a/docs/README-linux.md b/docs/README-linux.md
index a0ba1b8ae2d9..345e0acbc3a6 100644
--- a/docs/README-linux.md
+++ b/docs/README-linux.md
@@ -16,7 +16,7 @@ Ubuntu 18.04, all available features enabled:
 
     sudo apt-get install build-essential git make autoconf automake libtool \
     pkg-config cmake ninja-build gnome-desktop-testing libasound2-dev libpulse-dev \
-    libaudio-dev libsamplerate0-dev libx11-dev libxext-dev \
+    libaudio-dev libjack-dev libsndio-dev libsamplerate0-dev libx11-dev libxext-dev \
     libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libwayland-dev \
     libxkbcommon-dev libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev \
     libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev fcitx-libs-dev
@@ -32,7 +32,7 @@ Fedora 35, all available features enabled:
     systemd-devel mesa-libGL-devel libxkbcommon-devel mesa-libGLES-devel \
     mesa-libEGL-devel vulkan-devel wayland-devel wayland-protocols-devel \
     libdrm-devel mesa-libgbm-devel libusb-devel libdecor-devel \
-    libsamplerate-devel
+    libsamplerate-devel pipewire-jack-audio-connection-kit-devel \
 
 NOTES:
 - This includes all the audio targets except arts and esd, because Ubuntu
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 9aea5e9d2d74..fef07118ae66 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -299,6 +299,8 @@
 #cmakedefine SDL_AUDIO_DRIVER_DUMMY @SDL_AUDIO_DRIVER_DUMMY@
 #cmakedefine SDL_AUDIO_DRIVER_EMSCRIPTEN @SDL_AUDIO_DRIVER_EMSCRIPTEN@
 #cmakedefine SDL_AUDIO_DRIVER_HAIKU @SDL_AUDIO_DRIVER_HAIKU@
+#cmakedefine SDL_AUDIO_DRIVER_JACK @SDL_AUDIO_DRIVER_JACK@
+#cmakedefine SDL_AUDIO_DRIVER_JACK_DYNAMIC @SDL_AUDIO_DRIVER_JACK_DYNAMIC@
 #cmakedefine SDL_AUDIO_DRIVER_NETBSD @SDL_AUDIO_DRIVER_NETBSD@
 #cmakedefine SDL_AUDIO_DRIVER_OSS @SDL_AUDIO_DRIVER_OSS@
 #cmakedefine SDL_AUDIO_DRIVER_PIPEWIRE @SDL_AUDIO_DRIVER_PIPEWIRE@
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 185c10c5cd88..0bf7b6fc6061 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -285,6 +285,8 @@
 #undef SDL_AUDIO_DRIVER_DUMMY
 #undef SDL_AUDIO_DRIVER_EMSCRIPTEN
 #undef SDL_AUDIO_DRIVER_HAIKU
+#undef SDL_AUDIO_DRIVER_JACK
+#undef SDL_AUDIO_DRIVER_JACK_DYNAMIC
 #undef SDL_AUDIO_DRIVER_NETBSD
 #undef SDL_AUDIO_DRIVER_OPENSLES
 #undef SDL_AUDIO_DRIVER_OSS
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 001eecb95c25..5501ca141cbf 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -84,6 +84,9 @@ static const AudioBootStrap *const bootstrap[] = {
 #if SDL_AUDIO_DRIVER_EMSCRIPTEN
     &EMSCRIPTENAUDIO_bootstrap,
 #endif
+#if SDL_AUDIO_DRIVER_JACK
+    &JACK_bootstrap,
+#endif
 #if SDL_AUDIO_DRIVER_PIPEWIRE
     &PIPEWIRE_bootstrap,
 #endif
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 99870032049a..251ea9e3b601 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -184,6 +184,7 @@ typedef struct AudioBootStrap
 extern AudioBootStrap PIPEWIRE_bootstrap;
 extern AudioBootStrap PULSEAUDIO_bootstrap;
 extern AudioBootStrap ALSA_bootstrap;
+extern AudioBootStrap JACK_bootstrap;
 extern AudioBootStrap SNDIO_bootstrap;
 extern AudioBootStrap NETBSDAUDIO_bootstrap;
 extern AudioBootStrap DSP_bootstrap;
diff --git a/src/audio/jack/SDL_jackaudio.c b/src/audio/jack/SDL_jackaudio.c
new file mode 100644
index 000000000000..f6e7e0cd38a7
--- /dev/null
+++ b/src/audio/jack/SDL_jackaudio.c
@@ -0,0 +1,446 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#if SDL_AUDIO_DRIVER_JACK
+
+#include "SDL_timer.h"
+#include "SDL_audio.h"
+#include "../SDL_audio_c.h"
+#include "SDL_jackaudio.h"
+#include "SDL_loadso.h"
+#include "../../thread/SDL_systhread.h"
+
+
+static jack_client_t * (*JACK_jack_client_open) (const char *, jack_options_t, jack_status_t *, ...);
+static int (*JACK_jack_client_close) (jack_client_t *);
+static void (*JACK_jack_on_shutdown) (jack_client_t *, JackShutdownCallback, void *);
+static int (*JACK_jack_activate) (jack_client_t *);
+static int (*JACK_jack_deactivate) (jack_client_t *);
+static void * (*JACK_jack_port_get_buffer) (jack_port_t *, jack_nframes_t);
+static int (*JACK_jack_port_unregister) (jack_client_t *, jack_port_t *);
+static void (*JACK_jack_free) (void *);
+static const char ** (*JACK_jack_get_ports) (jack_client_t *, const char *, const char *, unsigned long);
+static jack_nframes_t (*JACK_jack_get_sample_rate) (jack_client_t *);
+static jack_nframes_t (*JACK_jack_get_buffer_size) (jack_client_t *);
+static jack_port_t * (*JACK_jack_port_register) (jack_client_t *, const char *, const char *, unsigned long, unsigned long);
+static jack_port_t * (*JACK_jack_port_by_name) (jack_client_t *, const char *);
+static const char * (*JACK_jack_port_name) (const jack_port_t *);
+static const char * (*JACK_jack_port_type) (const jack_port_t *);
+static int (*JACK_jack_connect) (jack_client_t *, const char *, const char *);
+static int (*JACK_jack_set_process_callback) (jack_client_t *, JackProcessCallback, void *);
+
+static int load_jack_syms(void);
+
+
+#ifdef SDL_AUDIO_DRIVER_JACK_DYNAMIC
+
+static const char *jack_library = SDL_AUDIO_DRIVER_JACK_DYNAMIC;
+static void *jack_handle = NULL;
+
+/* !!! FIXME: this is copy/pasted in several places now */
+static int
+load_jack_sym(const char *fn, void **addr)
+{
+    *addr = SDL_LoadFunction(jack_handle, fn);
+    if (*addr == NULL) {
+        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
+        return 0;
+    }
+
+    return 1;
+}
+
+/* cast funcs to char* first, to please GCC's strict aliasing rules. */
+#define SDL_JACK_SYM(x) \
+    if (!load_jack_sym(#x, (void **) (char *) &JACK_##x)) return -1
+
+static void
+UnloadJackLibrary(void)
+{
+    if (jack_handle != NULL) {
+        SDL_UnloadObject(jack_handle);
+        jack_handle = NULL;
+    }
+}
+
+static int
+LoadJackLibrary(void)
+{
+    int retval = 0;
+    if (jack_handle == NULL) {
+        jack_handle = SDL_LoadObject(jack_library);
+        if (jack_handle == NULL) {
+            retval = -1;
+            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
+        } else {
+            retval = load_jack_syms();
+            if (retval < 0) {
+                UnloadJackLibrary();
+            }
+        }
+    }
+    return retval;
+}
+
+#else
+
+#define SDL_JACK_SYM(x) JACK_##x = x
+
+static void
+UnloadJackLibrary(void)
+{
+}
+
+static int
+LoadJackLibrary(void)
+{
+    load_jack_syms();
+    return 0;
+}
+
+#endif /* SDL_AUDIO_DRIVER_JACK_DYNAMIC */
+
+
+static int
+load_jack_syms(void)
+{
+    SDL_JACK_SYM(jack_client_open);
+    SDL_JACK_SYM(jack_client_close);
+    SDL_JACK_SYM(jack_on_shutdown);
+    SDL_JACK_SYM(jack_activate);
+    SDL_JACK_SYM(jack_deactivate);
+    SDL_JACK_SYM(jack_port_get_buffer);
+    SDL_JACK_SYM(jack_port_unregister);
+    SDL_JACK_SYM(jack_free);
+    SDL_JACK_SYM(jack_get_ports);
+    SDL_JACK_SYM(jack_get_sample_rate);
+    SDL_JACK_SYM(jack_get_buffer_size);
+    SDL_JACK_SYM(jack_port_register);
+    SDL_JACK_SYM(jack_port_by_name);
+    SDL_JACK_SYM(jack_port_name);
+    SDL_JACK_SYM(jack_port_type);
+    SDL_JACK_SYM(jack_connect);
+    SDL_JACK_SYM(jack_set_process_callback);
+    return 0;
+}
+
+
+static void
+jackShutdownCallback(void *arg)  /* JACK went away; device is lost. */
+{
+    SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
+    SDL_OpenedAudioDeviceDisconnected(this);
+    SDL_SemPost(this->hidden->iosem);  /* unblock the SDL thread. */
+}
+
+// !!! FIXME: implement and register these!
+//typedef int(* JackSampleRateCallback)(jack_nframes_t nframes, void *arg)
+//typedef int(* JackBufferSizeCallback)(jack_nframes_t nframes, void *arg)
+
+static int
+jackProcessPlaybackCallback(jack_nframes_t nframes, void *arg)
+{
+    SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
+    jack_port_t **ports = this->hidden->sdlports;
+    const int total_channels = this->spec.channels;
+    const int total_frames = this->spec.samples;
+    int channelsi;
+
+    if (!SDL_AtomicGet(&this->enabled)) {
+        /* silence the buffer to avoid repeats and corruption. */
+        SDL_memset(this->hidden->iobuffer, '\0', this->spec.size);
+    }
+
+    for (channelsi = 0; channelsi < total_channels; channelsi++) {
+        float *dst = (float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
+        if (dst) {
+            const float *src = ((float *) this->hidden->iobuffer) + channelsi;
+            int framesi;
+            for (framesi = 0; framesi < total_frames; framesi++) {
+                *(dst++) = *src;
+                src += total_channels;
+            }
+        }
+    }
+
+    SDL_SemPost(this->hidden->iosem);  /* tell SDL thread we're done; refill the buffer. */
+    return 0;  /* success */
+}
+
+
+/* This function waits until it is possible to write a full sound buffer */
+static void
+JACK_WaitDevice(_THIS)
+{
+    if (SDL_AtomicGet(&this->enabled)) {
+        if (SDL_SemWait(this->hidden->iosem) == -1) {
+            SDL_OpenedAudioDeviceDisconnected(this);
+        }
+    }
+}
+
+static Uint8 *
+JACK_GetDeviceBuf(_THIS)
+{
+    return (Uint8 *) this->hidden->iobuffer;
+}
+
+
+static int
+jackProcessCaptureCallback(jack_nframes_t nframes, void *arg)
+{
+    SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
+    if (SDL_AtomicGet(&this->enabled)) {
+        jack_port_t **ports = this->hidden->sdlports;
+        const int total_channels = this->spec.channels;
+        const int total_frames = this->spec.samples;
+        int channelsi;
+    
+        for (channelsi = 0; channelsi < total_channels; channelsi++) {
+            const float *src = (const float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
+            if (src) {
+                float *dst = ((float *) this->hidden->iobuffer) + channelsi;
+                int framesi;
+                for (framesi = 0; framesi < total_frames; framesi++) {
+                    *dst = *(src++);
+                    dst += total_channels;
+                }
+            }
+        }
+    }
+
+    SDL_SemPost(this->hidden->iosem);  /* tell SDL thread we're done; new buffer is ready! */
+    return 0;  /* success */
+}
+
+static int
+JACK_CaptureFromDevice(_THIS, void *buffer, int buflen)
+{
+    SDL_assert(buflen == this->spec.size);  /* we always fill a full buffer. */
+
+    /* Wait for JACK to fill the iobuffer */
+    if (SDL_SemWait(this->hidden->iosem) == -1) {
+        return -1;
+    }
+
+    SDL_memcpy(buffer, this->hidden->iobuffer, buflen);
+    return buflen;
+}
+
+static void
+JACK_FlushCapture(_THIS)
+{
+    SDL_SemWait(this->hidden->iosem);
+}
+
+
+static void
+JACK_CloseDevice(_THIS)
+{
+    if (this->hidden->client) {
+        JACK_jack_deactivate(this->hidden->client);
+
+        if (this->hidden->sdlports) {
+            const int channels = this->spec.channels;
+            int i;
+            for (i = 0; i < channels; i++) {
+                JACK_jack_port_unregister(this->hidden->client, this->hidden->sdlports[i]);
+            }
+            SDL_free(this->hidden->sdlports);
+        }
+
+        JACK_jack_client_close(this->hidden->client);
+    }
+
+    if (this->hidden->iosem) {
+        SDL_DestroySemaphore(this->hidden->iosem);
+    }
+
+    SDL_free(this->hidden->iobuffer);
+    SDL_free(this->hidden);
+}
+
+static int
+JACK_OpenDevice(_THIS, const char *devname)
+{
+    /* Note that JACK uses "output" for capture devices (they output audio
+        data to us) and "input" for playback (we input audio data to them).
+        Likewise, SDL's playback port will be "output" (we write data out)
+        and capture will be "input" (we read data in). */
+    SDL_bool iscapture = this->iscapture;
+    const unsigned long sysportflags = iscapture ? JackPortIsOutput : JackPortIsInput;
+    const unsigned long sdlportflags = iscapture ? JackPortIsInput : JackPortIsOutput;
+    const JackProcessCallback callback = iscapture ? jackProcessCaptureCallback : jackProcessPlaybackCallback;
+    const char *sdlportstr = iscapture ? "input" : "output";
+    const char **devports = NULL;
+    int *audio_ports;
+    jack_client_t *client = NULL;
+    jack_status_t status;
+    int channels = 0;
+    int ports = 0;
+    int i;
+
+    /* Initialize all variables that we clean on shutdown */
+    this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof (*this->hidden));
+    if (this->hidden == NULL) {
+        return SDL_OutOfMemory();
+    }
+
+    /* !!! FIXME: we _still_ need an API to specify an app name */
+    client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
+    this->hidden->client = client;
+    if (client == NULL) {
+        return SDL_SetError("Can't open JACK client");
+    }
+
+    devports = JACK_jack_get_ports(client, NULL, NULL, JackPortIsPhysical | sysportflags);
+    if (!devports || !devports[0]) {
+        return SDL_SetError("No physical JACK ports available");
+    }
+
+    while (devports[++ports]) {
+        /* spin to count devports */
+    }
+
+    /* Filter out non-audio ports */
+    audio_ports = SDL_calloc(ports, sizeof *audio_ports);
+    for (i = 0; i < ports; i++) {
+        const jack_port_t *dport = JACK_jack_port_by_name(client, devports[i]);
+        const char *type = JACK_jack_port_type(dport);
+        const int len = SDL_strlen(type);
+        /* See if type ends with "audio" */
+        if (len >= 5 && !SDL_memcmp(type+len-5, "audio", 5)) {
+            audio_ports[channels++] = i;
+        }
+    }
+    if (channels == 0) {
+        return SDL_SetError("No physical JACK ports available");
+    }
+
+
+    /* !!! FIXME: docs say about buffer size: "This size may change, clients that depend on it must register a bufsize_callback so they will be notified if it does." */
+
+    /* Jack pretty much demands what it wants. */
+    this->spec.format = AUDIO_F32SYS;
+    this->spec.freq = JACK_jack_get_sample_rate(client);
+    this->spec.channels = channels;
+    this->spec.samples = JACK_jack_get_buffer_size(client);
+
+    SDL_CalculateAudioSpec(&this->spec);
+
+    this->hidden->iosem = SDL_CreateSemaphore(0);
+    if (!this->hidden->iosem) {
+        return -1;  /* error was set by SDL_CreateSemaphore */
+    }
+
+    this->hidden->iobuffer = (float *) SDL_calloc(1, this->spec.size);
+    if (!this->hidden->iobuffer) {
+        return SDL_OutOfMemory();
+    }
+
+    /* Build SDL's ports, which we will connect to the device ports. */
+    this->hidden->sdlports = (jack_port_t **) SDL_calloc(channels, sizeof (jack_port_t *));
+    if (this->hidden->sdlports == NULL) {
+        return SDL_OutOfMemory();
+    }
+
+    for (i = 0; i < channels; i++) {
+        char portname[32];
+        SDL_snprintf(portname, sizeof (portname), "sdl_jack_%s_%d", sdlportstr, i);
+        this->hidden->sdlports[i] = JACK_jack_port_register(client, portname, JACK_DEFAULT_AUDIO_TYPE, sdlportflags, 0);
+        if (this->hidden->sdlports[i] == NULL) {
+            return SDL_SetError("jack_port_register failed");
+        }
+    }
+
+    if (JACK_jack_set_process_callback(client, callback, this) != 0) {
+        return SDL_SetError("JACK: Couldn't set process callback");
+    }
+
+    JACK_jack_on_shutdown(client, jackShutdownCallback, this);
+
+    if (JACK_jack_activate(client) != 0) {
+        return SDL_SetError("Failed to activate JACK client");
+    }
+
+    /* once activated, we can connect all the ports. */
+    for (i = 0; i < channels; i++) {
+        const char *sdlport = JACK_jack_port_name(this->hidden->sdlports[i]);
+        const char *srcport = iscapture ? devports[audio_ports[i]] : sdlport;
+        const char *dstport = iscapture ? sdlport : devports[audio_ports[i]];
+        if (JACK_jack_connect(client, srcport, dstport) != 0) {
+            return SDL_SetError("Couldn't connect JACK ports: %s => %s", srcport, dstport);
+        }
+    }
+
+    /* don't need these anymore. */
+    JACK_jack_free(devports);
+    SDL_free(audio_ports);
+
+    /* We're ready to rock and roll. :-) */
+    return 0;
+}
+
+static void
+JACK_Deinitialize(void)
+{
+    UnloadJackLibrary();
+}
+
+static SDL_bool
+JACK_Init(SDL_AudioDriverImpl * impl)
+{
+    if (LoadJackLibrary() < 0) {
+        return SDL_FALSE;
+    } else {
+        /* Make sure a JACK server is running and available. */
+        jack_status_t status;
+        jack_client_t *client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
+        if (client == NULL) {
+            UnloadJackLibrary();
+            return SDL_FALSE;
+        }
+        JACK_jack_client_close(client);
+    }
+
+    /* Set the function pointers */
+    impl->OpenDevice = JACK_OpenDevice;
+    impl->WaitDevice = JACK_WaitDevice;
+    impl->GetDeviceBuf = JACK_GetDeviceBuf;
+    impl->CloseDevice = JACK_CloseDevice;
+    impl->Deinitialize = JACK_Deinitialize;
+    impl->CaptureFromDevice = JACK_CaptureFromDevice;
+    impl->FlushCapture = JACK_FlushCapture;
+    impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
+    impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
+    impl->HasCaptureSupport = SDL_TRUE;
+
+    return SDL_TRUE;   /* this au

(Patch may be truncated, please check the link at the top of this post.)