From ef81af9245bf74c8e2c9e347697c521afcf7a594 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 19 Apr 2002 20:08:54 +0000 Subject: [PATCH] the "two birds with one stone" commit 1) when /not/ building static plugins, the plugin info name is, once again, PluginInfo, but is still type_name_PluginInfo for static plugins. This allows plugins to be symlinked (highly desirable for the debian packages, and likely others). This also requires plugins to /not/ be loaded with RTLD_GLOBAL. 2) because of 1, snd_alsa_0_9 has to explicitly load libasound itself. This just happens to fix my segfault on shutdown in RedHat 6.2. --- configure.ac | 1 + include/QF/plugin.h | 6 ++ include/alsa_funcs_list.h | 64 ++++++++++++++ libs/audio/cd/cd_linux.c | 2 +- libs/audio/cd/cd_null.c | 2 +- libs/audio/cd/cd_sdl.c | 2 +- libs/audio/cd/cd_sgi.c | 2 +- libs/audio/cd/cd_win.c | 2 +- libs/audio/cd/cd_xmms.c | 2 +- libs/audio/renderer/snd_dma.c | 2 +- libs/audio/renderer/snd_null.c | 6 +- libs/audio/targets/Makefile.am | 2 +- libs/audio/targets/snd_alsa_0_5.c | 2 +- libs/audio/targets/snd_alsa_0_9.c | 120 ++++++++++++++++--------- libs/audio/targets/snd_disk.c | 2 +- libs/audio/targets/snd_null.c | 6 +- libs/audio/targets/snd_oss.c | 2 +- libs/audio/targets/snd_sdl.c | 2 +- libs/audio/targets/snd_sgi.c | 2 +- libs/audio/targets/snd_sun.c | 2 +- libs/audio/targets/snd_win.c | 2 +- libs/console/client.c | 2 +- libs/console/server.c | 2 +- libs/util/plugin.c | 140 +++++++++++++++++++----------- 24 files changed, 266 insertions(+), 111 deletions(-) create mode 100644 include/alsa_funcs_list.h diff --git a/configure.ac b/configure.ac index ca3cda4b7..be1c9d03e 100644 --- a/configure.ac +++ b/configure.ac @@ -1731,6 +1731,7 @@ if test "x$static_plugins" = xauto -a "x$SYSTYPE" = xWIN32; then static_plugins=yes fi if test "x$static_plugins" = xyes; then + AC_DEFINE(STATIC_PLUGINS, 1, [Define this if you are building static plugins]) PLUGIN_RPATH='' SERVER_PLUGIN_STATIC="$SERVER_PLUGIN_TARGETS" SERVER_PLUGIN_TARGETS="" diff --git a/include/QF/plugin.h b/include/QF/plugin.h index 10b21e4e0..fa435db60 100644 --- a/include/QF/plugin.h +++ b/include/QF/plugin.h @@ -49,6 +49,12 @@ #include #include +#ifdef STATIC_PLUGINS +#define PLUGIN_INFO(type,name) type##_##name##_PluginInfo +#else +#define PLUGIN_INFO(type,name) PluginInfo +#endif + typedef enum { qfp_null = 0, // Not real qfp_input, // Input (pointing devices, joysticks, etc) diff --git a/include/alsa_funcs_list.h b/include/alsa_funcs_list.h new file mode 100644 index 000000000..6d778f4fa --- /dev/null +++ b/include/alsa_funcs_list.h @@ -0,0 +1,64 @@ +/* + alsa_funcs_list.h + + QF ALSA function list + + Copyright (C) 2001 Bill Currie + + Author: Bill Currie + Date: 2002/4/19 + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program 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 General Public License + along with this program; if not, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + + $Id$ +*/ + +#ifndef QF_ALSA_NEED +#define QF_ALSA_NEED(ret, func, params) +#define UNDEF_QF_ALSA_NEED +#endif + +QF_ALSA_NEED (int, snd_pcm_close, (snd_pcm_t *pcm)) +QF_ALSA_NEED (int, snd_pcm_hw_params, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params)) +QF_ALSA_NEED (int, snd_pcm_hw_params_any, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params)) +QF_ALSA_NEED (snd_pcm_sframes_t, snd_pcm_hw_params_get_buffer_size, (const snd_pcm_hw_params_t *params)) +QF_ALSA_NEED (snd_pcm_sframes_t, snd_pcm_hw_params_get_period_size, (const snd_pcm_hw_params_t *params, int *dir)) +QF_ALSA_NEED (int, snd_pcm_hw_params_set_access, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t val)) +QF_ALSA_NEED (int, snd_pcm_hw_params_set_channels, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)) +QF_ALSA_NEED (int, snd_pcm_hw_params_set_format, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val)) +QF_ALSA_NEED (snd_pcm_uframes_t, snd_pcm_hw_params_set_period_size_near, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int *dir)) +QF_ALSA_NEED (unsigned int, snd_pcm_hw_params_set_rate_near, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int *dir)) +QF_ALSA_NEED (size_t, snd_pcm_hw_params_sizeof, (void)) +QF_ALSA_NEED (int, snd_pcm_mmap_begin, (snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)) +QF_ALSA_NEED (snd_pcm_sframes_t, snd_pcm_mmap_commit, (snd_pcm_t *pcm, snd_pcm_uframes_t offset, snd_pcm_uframes_t frames)) +QF_ALSA_NEED (int, snd_pcm_open, (snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode)) +QF_ALSA_NEED (int, snd_pcm_pause, (snd_pcm_t *pcm, int enable)) +QF_ALSA_NEED (int, snd_pcm_start, (snd_pcm_t *pcm)) +QF_ALSA_NEED (snd_pcm_state_t, snd_pcm_state, (snd_pcm_t *pcm)) +QF_ALSA_NEED (int, snd_pcm_sw_params, (snd_pcm_t *pcm, snd_pcm_sw_params_t *params)) +QF_ALSA_NEED (int, snd_pcm_sw_params_current, (snd_pcm_t *pcm, snd_pcm_sw_params_t *params)) +QF_ALSA_NEED (int, snd_pcm_sw_params_set_start_threshold, (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)) +QF_ALSA_NEED (int, snd_pcm_sw_params_set_stop_threshold, (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)) +QF_ALSA_NEED (size_t, snd_pcm_sw_params_sizeof, (void)) +QF_ALSA_NEED (const char *, snd_strerror, (int errnum)) + +#ifdef UNDEF_QF_ALSA_NEED +#undef QF_ALSA_NEED +#undef UNDEF_QF_ALSA_NEED +#endif diff --git a/libs/audio/cd/cd_linux.c b/libs/audio/cd/cd_linux.c index e5b8c962c..e9d5008b9 100644 --- a/libs/audio/cd/cd_linux.c +++ b/libs/audio/cd/cd_linux.c @@ -452,7 +452,7 @@ I_CDAudio_Init (void) } QFPLUGIN plugin_t * -cd_linux_PluginInfo (void) +PLUGIN_INFO(cd, linux) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/cd/cd_null.c b/libs/audio/cd/cd_null.c index 805e41017..1fec477a8 100644 --- a/libs/audio/cd/cd_null.c +++ b/libs/audio/cd/cd_null.c @@ -79,7 +79,7 @@ I_CD_f (void) } QFPLUGIN plugin_t * -cd_null_PluginInfo (void) +PLUGIN_INFO(cd, null) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/cd/cd_sdl.c b/libs/audio/cd/cd_sdl.c index 7688a53cf..29bb6ea3a 100644 --- a/libs/audio/cd/cd_sdl.c +++ b/libs/audio/cd/cd_sdl.c @@ -278,7 +278,7 @@ I_CDAudio_Init (void) } QFPLUGIN plugin_t * -cd_sdl_PluginInfo (void) +PLUGIN_INFO(cd, sdl) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/cd/cd_sgi.c b/libs/audio/cd/cd_sgi.c index d31869556..2e04545fe 100644 --- a/libs/audio/cd/cd_sgi.c +++ b/libs/audio/cd/cd_sgi.c @@ -363,7 +363,7 @@ I_SGI_Init (void) } QFPLUGIN plugin_t * -cd_sgi_PluginInfo (void) +PLUGIN_INFO(cd, sgi) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/cd/cd_win.c b/libs/audio/cd/cd_win.c index 601d2949b..9d77ac129 100644 --- a/libs/audio/cd/cd_win.c +++ b/libs/audio/cd/cd_win.c @@ -497,7 +497,7 @@ I_CDAudio_Init (void) } QFPLUGIN plugin_t * -cd_win_PluginInfo (void) +PLUGIN_INFO(cd, win) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/cd/cd_xmms.c b/libs/audio/cd/cd_xmms.c index 8d95d3605..abfc6c8d5 100644 --- a/libs/audio/cd/cd_xmms.c +++ b/libs/audio/cd/cd_xmms.c @@ -534,7 +534,7 @@ I_XMMS_f (void) } QFPLUGIN plugin_t * -cd_xmms_PluginInfo (void) +PLUGIN_INFO(cd, xmms) (void) { plugin_info.type = qfp_cd; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/renderer/snd_dma.c b/libs/audio/renderer/snd_dma.c index 17b493a0a..e48c0644b 100644 --- a/libs/audio/renderer/snd_dma.c +++ b/libs/audio/renderer/snd_dma.c @@ -967,7 +967,7 @@ SND_UnblockSound (void) } QFPLUGIN plugin_t * -snd_render_default_PluginInfo (void) +PLUGIN_INFO(snd_render, default) (void) { plugin_info.type = qfp_snd_render; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/renderer/snd_null.c b/libs/audio/renderer/snd_null.c index 0d17d8dc2..8f9587a1d 100644 --- a/libs/audio/renderer/snd_null.c +++ b/libs/audio/renderer/snd_null.c @@ -30,6 +30,10 @@ static const char rcsid[] = "$Id$"; +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "QF/cvar.h" #include "QF/plugin.h" #include "QF/sound.h" @@ -164,7 +168,7 @@ SND_UnblockSound (void) } QFPLUGIN plugin_t * -snd_render_null_PluginInfo (void) +PLUGIN_INFO(snd_render, null) (void) { plugin_info.type = qfp_sound; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/targets/Makefile.am b/libs/audio/targets/Makefile.am index a5124134c..25d98f699 100644 --- a/libs/audio/targets/Makefile.am +++ b/libs/audio/targets/Makefile.am @@ -20,7 +20,7 @@ libsnd_output_alsa0_5_la_LIBADD= $(ALSA_LIBS) libsnd_output_alsa0_5_la_SOURCES= snd_alsa_0_5.c libsnd_output_alsa0_9_la_LDFLAGS= -version-info 1:0:0 $(PLUGIN_RPATH) -libsnd_output_alsa0_9_la_LIBADD= $(ALSA_LIBS) +libsnd_output_alsa0_9_la_LIBADD= libsnd_output_alsa0_9_la_SOURCES= snd_alsa_0_9.c libsnd_output_oss_la_LDFLAGS= -version-info 1:0:0 $(PLUGIN_RPATH) diff --git a/libs/audio/targets/snd_alsa_0_5.c b/libs/audio/targets/snd_alsa_0_5.c index f954d3e7b..1e9afb17c 100644 --- a/libs/audio/targets/snd_alsa_0_5.c +++ b/libs/audio/targets/snd_alsa_0_5.c @@ -404,7 +404,7 @@ SNDDMA_UnblockSound (void) } plugin_t * -snd_output_alsa0_5_PluginInfo (void) { +PLUGIN_INFO(snd_output, alsa0_5) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/audio/targets/snd_alsa_0_9.c b/libs/audio/targets/snd_alsa_0_9.c index e2ac27960..0ebea49bb 100644 --- a/libs/audio/targets/snd_alsa_0_9.c +++ b/libs/audio/targets/snd_alsa_0_9.c @@ -33,6 +33,7 @@ static const char rcsid[] = #endif #include +#include #include #include "QF/cvar.h" @@ -62,9 +63,37 @@ static general_funcs_t plugin_info_general_funcs; static snd_output_data_t plugin_info_snd_output_data; static snd_output_funcs_t plugin_info_snd_output_funcs; +static void *alsa_handle; -void -static SNDDMA_Init_Cvars (void) +#define QF_ALSA_NEED(ret, func, params) \ +static ret (*qf##func) params; +#include "alsa_funcs_list.h" +#undef QF_ALSA_NEED + +static qboolean +load_libasound (void) +{ + if (!(alsa_handle = dlopen ("libasound.so.2", RTLD_GLOBAL | RTLD_NOW))) { + Sys_Printf ("couldn't load libasound.so.2: %s\n", dlerror ()); + return false; + } +#define QF_ALSA_NEED(ret, func, params) \ + if (!(qf##func = dlsym (alsa_handle, #func))) { \ + Sys_Printf ("Couldn't load ALSA function %s\n", #func); \ + dlclose (alsa_handle); \ + alsa_handle = 0; \ + return false; \ + } +#include "alsa_funcs_list.h" +#undef QF_ALSA_NEED + return true; +} + +#define snd_pcm_hw_params_sizeof qfsnd_pcm_hw_params_sizeof +#define snd_pcm_sw_params_sizeof qfsnd_pcm_sw_params_sizeof + +static void +SNDDMA_Init_Cvars (void) { snd_stereo = Cvar_Get ("snd_stereo", "1", CVAR_ROM, NULL, "sound stereo output"); @@ -86,6 +115,9 @@ SNDDMA_Init (void) snd_pcm_hw_params_t *hw; snd_pcm_sw_params_t *sw; + if (!load_libasound ()) + return false; + snd_pcm_hw_params_alloca (&hw); snd_pcm_sw_params_alloca (&sw); @@ -108,26 +140,27 @@ SNDDMA_Init (void) stereo = snd_stereo->int_val; if (!pcmname) pcmname = "plughw:0,0"; - if ((err = snd_pcm_open (&pcm, pcmname, - SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) { - Sys_Printf ("Error: audio open error: %s\n", snd_strerror (err)); + if ((err = qfsnd_pcm_open (&pcm, pcmname, + SND_PCM_STREAM_PLAYBACK, + SND_PCM_NONBLOCK)) < 0) { + Sys_Printf ("Error: audio open error: %s\n", qfsnd_strerror (err)); return 0; } Sys_Printf ("Using PCM %s.\n", pcmname); - snd_pcm_hw_params_any (pcm, hw); + qfsnd_pcm_hw_params_any (pcm, hw); switch (rate) { case -1: - if (snd_pcm_hw_params_set_rate_near (pcm, hw, 44100, 0) >= 0) { + if (qfsnd_pcm_hw_params_set_rate_near (pcm, hw, 44100, 0) >= 0) { frag_size = 256; /* assuming stereo 8 bit */ rate = 44100; - } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 22050, 0) >= - 0) { + } else if (qfsnd_pcm_hw_params_set_rate_near (pcm, hw, + 22050, 0) >= 0) { frag_size = 128; /* assuming stereo 8 bit */ rate = 22050; - } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 11025, 0) >= - 0) { + } else if (qfsnd_pcm_hw_params_set_rate_near (pcm, hw, + 11025, 0) >= 0) { frag_size = 64; /* assuming stereo 8 bit */ rate = 11025; } else { @@ -138,7 +171,7 @@ SNDDMA_Init (void) case 11025: case 22050: case 44100: - if (snd_pcm_hw_params_set_rate_near (pcm, hw, rate, 0) >= 0) { + if (qfsnd_pcm_hw_params_set_rate_near (pcm, hw, rate, 0) >= 0) { frag_size = 64 * rate / 11025; /* assuming stereo 8 bit */ break; } @@ -150,11 +183,11 @@ SNDDMA_Init (void) switch (bps) { case -1: - if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_S16_LE) - >= 0) { + if (qfsnd_pcm_hw_params_set_format (pcm, hw, + SND_PCM_FORMAT_S16_LE) >= 0) { bps = 16; - } else if (snd_pcm_hw_params_set_format (pcm, hw, - SND_PCM_FORMAT_U8) + } else if (qfsnd_pcm_hw_params_set_format (pcm, hw, + SND_PCM_FORMAT_U8) >= 0) { bps = 8; } else { @@ -164,9 +197,9 @@ SNDDMA_Init (void) break; case 8: case 16: - if (snd_pcm_hw_params_set_format (pcm, hw, - bps == 8 ? SND_PCM_FORMAT_U8 : - SND_PCM_FORMAT_S16) >= 0) { + if (qfsnd_pcm_hw_params_set_format (pcm, hw, + bps == 8 ? SND_PCM_FORMAT_U8 : + SND_PCM_FORMAT_S16) >= 0) { break; } /* Fall through */ @@ -175,17 +208,17 @@ SNDDMA_Init (void) goto error; } - if (snd_pcm_hw_params_set_access (pcm, hw, - SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) { + if (qfsnd_pcm_hw_params_set_access (pcm, hw, + SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) { Sys_Printf ("ALSA: interleaved is not supported\n"); goto error; } switch (stereo) { case -1: - if (snd_pcm_hw_params_set_channels (pcm, hw, 2) >= 0) { + if (qfsnd_pcm_hw_params_set_channels (pcm, hw, 2) >= 0) { stereo = 1; - } else if (snd_pcm_hw_params_set_channels (pcm, hw, 1) >= 0) { + } else if (qfsnd_pcm_hw_params_set_channels (pcm, hw, 1) >= 0) { stereo = 0; } else { Sys_Printf ("ALSA: no useable channels\n"); @@ -194,7 +227,7 @@ SNDDMA_Init (void) break; case 0: case 1: - if (snd_pcm_hw_params_set_channels (pcm, hw, stereo ? 2 : 1) >= 0) + if (qfsnd_pcm_hw_params_set_channels (pcm, hw, stereo ? 2 : 1) >= 0) break; /* Fall through */ default: @@ -202,19 +235,19 @@ SNDDMA_Init (void) goto error; } - snd_pcm_hw_params_set_period_size_near (pcm, hw, frag_size, 0); + qfsnd_pcm_hw_params_set_period_size_near (pcm, hw, frag_size, 0); - err = snd_pcm_hw_params (pcm, hw); + err = qfsnd_pcm_hw_params (pcm, hw); if (err < 0) { Sys_Printf ("ALSA: unable to install hw params\n"); goto error; } - snd_pcm_sw_params_current (pcm, sw); - snd_pcm_sw_params_set_start_threshold (pcm, sw, ~0U); - snd_pcm_sw_params_set_stop_threshold (pcm, sw, ~0U); + qfsnd_pcm_sw_params_current (pcm, sw); + qfsnd_pcm_sw_params_set_start_threshold (pcm, sw, ~0U); + qfsnd_pcm_sw_params_set_stop_threshold (pcm, sw, ~0U); - err = snd_pcm_sw_params (pcm, sw); + err = qfsnd_pcm_sw_params (pcm, sw); if (err < 0) { Sys_Printf ("ALSA: unable to install sw params\n"); goto error; @@ -224,11 +257,12 @@ SNDDMA_Init (void) memset ((dma_t *) shm, 0, sizeof (*shm)); shm->splitbuffer = 0; shm->channels = stereo + 1; - shm->submission_chunk = snd_pcm_hw_params_get_period_size (hw, 0); // don't + shm->submission_chunk = qfsnd_pcm_hw_params_get_period_size (hw, 0); + // don't // mix less than this # shm->samplepos = 0; // in mono samples shm->samplebits = bps; - buffer_size = snd_pcm_hw_params_get_buffer_size (hw); + buffer_size = qfsnd_pcm_hw_params_get_buffer_size (hw); shm->samples = buffer_size * shm->channels; // mono samples in buffer shm->speed = rate; SNDDMA_GetDMAPos ();//XXX sets shm->buffer @@ -244,7 +278,7 @@ SNDDMA_Init (void) snd_inited = 1; return 1; error: - snd_pcm_close (pcm); + qfsnd_pcm_close (pcm); return 0; } @@ -258,7 +292,7 @@ SNDDMA_GetDMAPos (void) if (!snd_inited) return 0; - snd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); + qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); offset *= shm->channels; nframes *= shm->channels; shm->samplepos = offset; @@ -270,7 +304,7 @@ static void SNDDMA_Shutdown (void) { if (snd_inited) { - snd_pcm_close (pcm); + qfsnd_pcm_close (pcm); snd_inited = 0; } } @@ -295,17 +329,17 @@ SNDDMA_Submit (void) nframes = count / shm->channels; - snd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); + qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); - state = snd_pcm_state (pcm); + state = qfsnd_pcm_state (pcm); switch (state) { case SND_PCM_STATE_PREPARED: - snd_pcm_mmap_commit (pcm, offset, nframes); - snd_pcm_start (pcm); + qfsnd_pcm_mmap_commit (pcm, offset, nframes); + qfsnd_pcm_start (pcm); break; case SND_PCM_STATE_RUNNING: - snd_pcm_mmap_commit (pcm, offset, nframes); + qfsnd_pcm_mmap_commit (pcm, offset, nframes); break; default: break; @@ -316,7 +350,7 @@ static void SNDDMA_BlockSound (void) { if (++snd_blocked == 1) - snd_pcm_pause (pcm, 1); + qfsnd_pcm_pause (pcm, 1); } static void @@ -325,11 +359,11 @@ SNDDMA_UnblockSound (void) if (!snd_blocked) return; if (!--snd_blocked) - snd_pcm_pause (pcm, 0); + qfsnd_pcm_pause (pcm, 0); } plugin_t * -snd_output_alsa0_9_PluginInfo (void) +PLUGIN_INFO(snd_output, alsa0_9) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/targets/snd_disk.c b/libs/audio/targets/snd_disk.c index 494166369..10b139320 100644 --- a/libs/audio/targets/snd_disk.c +++ b/libs/audio/targets/snd_disk.c @@ -153,7 +153,7 @@ SNDDMA_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_disk_PluginInfo (void) { +PLUGIN_INFO(snd_output, disk) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/audio/targets/snd_null.c b/libs/audio/targets/snd_null.c index 0420664ea..b2f09a5d7 100644 --- a/libs/audio/targets/snd_null.c +++ b/libs/audio/targets/snd_null.c @@ -30,6 +30,10 @@ static const char rcsid[] = "$Id$"; +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "QF/cvar.h" #include "QF/plugin.h" #include "QF/sound.h" @@ -167,7 +171,7 @@ SND_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_null_PluginInfo (void) { +PLUGIN_INFO(snd_output, null) (void) { plugin_info.type = qfp_sound; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/audio/targets/snd_oss.c b/libs/audio/targets/snd_oss.c index d07afad1a..833394b3c 100644 --- a/libs/audio/targets/snd_oss.c +++ b/libs/audio/targets/snd_oss.c @@ -378,7 +378,7 @@ SNDDMA_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_oss_PluginInfo (void) { +PLUGIN_INFO(snd_output, oss) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/audio/targets/snd_sdl.c b/libs/audio/targets/snd_sdl.c index 3b73269ad..9620e2c91 100644 --- a/libs/audio/targets/snd_sdl.c +++ b/libs/audio/targets/snd_sdl.c @@ -229,7 +229,7 @@ SNDDMA_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_sdl_PluginInfo (void) +PLUGIN_INFO(snd_output, sdl) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/targets/snd_sgi.c b/libs/audio/targets/snd_sgi.c index 9d16d3b29..ddf5080e0 100644 --- a/libs/audio/targets/snd_sgi.c +++ b/libs/audio/targets/snd_sgi.c @@ -340,7 +340,7 @@ SNDDMA_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_sgi_PluginInfo (void) +PLUGIN_INFO(snd_output, sgi) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; diff --git a/libs/audio/targets/snd_sun.c b/libs/audio/targets/snd_sun.c index c31f971e9..9d229cb1d 100644 --- a/libs/audio/targets/snd_sun.c +++ b/libs/audio/targets/snd_sun.c @@ -252,7 +252,7 @@ SNDDMA_UnblockSound (void) } QFPLUGIN plugin_t * -snd_output_sun_PluginInfo (void) { +PLUGIN_INFO(snd_output, sun) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/audio/targets/snd_win.c b/libs/audio/targets/snd_win.c index cd9718e98..76b5fa462 100644 --- a/libs/audio/targets/snd_win.c +++ b/libs/audio/targets/snd_win.c @@ -719,7 +719,7 @@ DSOUND_Restore (void) } QFPLUGIN plugin_t * -snd_output_win_PluginInfo (void) { +PLUGIN_INFO(snd_output, win) (void) { plugin_info.type = qfp_snd_output; plugin_info.api_version = QFPLUGIN_VERSION; plugin_info.plugin_version = "0.1"; diff --git a/libs/console/client.c b/libs/console/client.c index f0341a18e..abd15936d 100644 --- a/libs/console/client.c +++ b/libs/console/client.c @@ -811,7 +811,7 @@ static plugin_t plugin_info = { }; QFPLUGIN plugin_t * -console_client_PluginInfo (void) +PLUGIN_INFO(console, client) (void) { return &plugin_info; } diff --git a/libs/console/server.c b/libs/console/server.c index 9f186b151..e8479b8b9 100644 --- a/libs/console/server.c +++ b/libs/console/server.c @@ -479,7 +479,7 @@ static plugin_t plugin_info = { }; QFPLUGIN plugin_t * -console_server_PluginInfo (void) +PLUGIN_INFO(console, server) (void) { return &plugin_info; } diff --git a/libs/util/plugin.c b/libs/util/plugin.c index 36c1caeb1..855d353ad 100644 --- a/libs/util/plugin.c +++ b/libs/util/plugin.c @@ -65,12 +65,79 @@ cvar_t *fs_pluginpath; hashtab_t *registered_plugins; +static const char *pi_error = ""; + static const char * plugin_get_key (void *pl, void *unused) { return ((plugin_list_t *) pl)->name; } +static int +pi_close_lib (void *handle) +{ +#if defined(HAVE_DLOPEN) + return (dlclose (handle) == 0); +#elif defined (_WIN32) + return (FreeLibrary (handle) == 0); +#endif +} + +static void * +pi_get_symbol (void *handle, const char *name) +{ +#if defined(HAVE_DLOPEN) + return dlsym (handle, name); +#elif defined (_WIN32) + return GetProcAddress (handle, name); +#endif +} + +static void * +pi_open_lib (const char *name) +{ + void *dlhand; + +#if defined(HAVE_DLOPEN) + if (!(dlhand = dlopen (name, RTLD_NOW))) { + pi_error = dlerror (); + return 0; + } +#elif defined (_WIN32) + if (!(dlhand = LoadLibrary (name))) { // lib not found + pi_error = "LoadLibrary failed"; + return 0; + } +#endif + pi_error = ""; + return dlhand; +} + +static void +pi_realname (char *realname, int size, const char *type, const char *name) +{ +#if defined(HAVE_DLOPEN) + const char *format = "%s/lib%s_%s.so"; +#elif defined(_WIN32) + const char *format = "%s/QF%s_%s.dll"; +#else +# error "No shared library support. FIXME" +#endif + + snprintf (realname, size, format, fs_pluginpath->string, type, name); +} + +static void +pi_info_name (char *info_name, int size, const char *type, const char *name) +{ + if (type && name) + snprintf (info_name, size, "%s_%s_PluginInfo", type, name); + else if (type) + snprintf (info_name, size, "%s_PluginInfo", type); + else + snprintf (info_name, size, "PluginInfo"); +} + void PI_InitCvars (void) { @@ -120,61 +187,39 @@ PI_LoadPlugin (const char *type, const char *name) plugin_info = pl->info; } if (!plugin_info) { - // Build the plugin info name - snprintf (plugin_info_name, sizeof (plugin_info_name), - "%s_%s_PluginInfo", type, name); // Build the path to the file to load -#if defined(HAVE_DLOPEN) - snprintf (realname, sizeof (realname), "%s/lib%s_%s.so", - fs_pluginpath->string, type, + pi_realname (realname, sizeof (realname), type, (tmpname ? tmpname + 1 : name)); -#elif defined(_WIN32) - snprintf (realname, sizeof (realname), "%s/QF%s_%s.dll", - fs_pluginpath->string, type, - (tmpname ? tmpname + 1 : name)); -#else -# error "No shared library support. FIXME" - return NULL; -#endif -#if defined(HAVE_DLOPEN) - if (!(dlhand = dlopen (realname, RTLD_GLOBAL | RTLD_NOW))) { + if (!(dlhand = pi_open_lib (realname))) { // lib not found Sys_Printf ("Could not load plugin \"%s\".\n", realname); - Sys_DPrintf ("Reason: \"%s\".\n", dlerror ()); + Sys_DPrintf ("Reason: \"%s\".\n", pi_error); return NULL; } -#elif defined (_WIN32) - if (!(dlhand = LoadLibrary (realname))) { // lib not found - Sys_Printf ("Could not load plugin \"%s\".\n", realname); - return NULL; - } -#endif -#if defined(HAVE_DLOPEN) - if (!(plugin_info = dlsym (dlhand, plugin_info_name))) { - // info function not found - dlclose (dlhand); - Sys_Printf ("Plugin info function not found\n"); - return NULL; + // Build the plugin info name as $type_$name_PluginInfo + pi_info_name (plugin_info_name, sizeof (plugin_info_name), type, name); + if (!(plugin_info = pi_get_symbol (dlhand, plugin_info_name))) { + // Build the plugin info name as $type_PluginInfo + pi_info_name (plugin_info_name, sizeof (plugin_info_name), + type, 0); + if (!(plugin_info = pi_get_symbol (dlhand, plugin_info_name))) { + // Build the plugin info name as PluginInfo + pi_info_name (plugin_info_name, sizeof (plugin_info_name), + 0, 0); + if (!(plugin_info = pi_get_symbol (dlhand, plugin_info_name))) { + // info function not found + pi_close_lib (dlhand); + Sys_Printf ("Plugin info function not found\n"); + return NULL; + } + } } -#elif defined (_WIN32) - if (!(plugin_info = (P_PluginInfo) GetProcAddress (dlhand, - plugin_info_name))) { - // info function not found - FreeLibrary (dlhand); - Sys_Printf ("Plugin info function not found\n"); - return NULL; - } -#endif } if (!(plugin = plugin_info ())) { // Something went badly wrong -#if defined(HAVE_DLOPEN) - dlclose (dlhand); -#elif defined (_WIN32) - FreeLibrary (dlhand); -#endif + pi_close_lib (dlhand); Sys_Printf ("Something went badly wrong.\n"); return NULL; } @@ -192,15 +237,12 @@ PI_UnloadPlugin (plugin_t *plugin) && plugin->functions->general->p_Shutdown) { plugin->functions->general->p_Shutdown (); } else { - Sys_DPrintf ("Warning: No shutdown function for type %d plugin!\n", plugin->type); + Sys_DPrintf ("Warning: No shutdown function for type %d plugin!\n", + plugin->type); } if (!plugin->handle) // we didn't load it return true; -#if defined(HAVE_DLOPEN) - return (dlclose (plugin->handle) == 0); -#elif defined (_WIN32) - return (FreeLibrary (plugin->handle) == 0); -#endif + return pi_close_lib (plugin->handle); } void