diff --git a/Makefile.in b/Makefile.in index 6fdb52779..8bd0fdf0c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -113,6 +113,7 @@ COMMON_OBJS = \ src/capture_filter/none.o \ src/capture_filter/split.o \ src/compat/alarm.o \ + src/compat/dlfunc.o \ src/compat/drand48.o \ src/compat/gettimeofday.o \ src/compat/platform_pipe.o \ diff --git a/src/compat/dlfunc.c b/src/compat/dlfunc.c new file mode 100644 index 000000000..c08d41bb2 --- /dev/null +++ b/src/compat/dlfunc.c @@ -0,0 +1,59 @@ +/** + * @file compat/dlfunc.c + * @author Martin Pulec + */ +/* + * Copyright (c) 2012-2022 CESNET, z. s. p. o. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, is permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of CESNET nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#include "config_unix.h" +#include "config_win32.h" +#endif // HAVE_CONFIG_H + +#include "dlfunc.h" + +#ifdef _WIN32 +char *dlerror() { + _Thread_local static char buf[1024] = "(unknown)"; + FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags + NULL, // lpsource + GetLastError(), // message id + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid + buf, // output buffer + sizeof buf, // size of msgbuf, bytes + NULL); // va_list of arguments + return buf; +} +#endif + diff --git a/src/compat/dlfunc.h b/src/compat/dlfunc.h new file mode 100644 index 000000000..09de3cf27 --- /dev/null +++ b/src/compat/dlfunc.h @@ -0,0 +1,55 @@ +/** + * @file compat/dlfunc.h + * @author Martin Pulec + */ +/* + * Copyright (c) 2011-2022 CESNET, z. s. p. o. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, is permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of CESNET nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef COMPAT_DLFUNC_H_08AEAEE7_485F_4969_8FDF_FBC2E4A78713 +#define COMPAT_DLFUNC_H_08AEAEE7_485F_4969_8FDF_FBC2E4A78713 + +#ifdef _WIN32 +#include "config_common.h" // EXTERN_C +#include +#define LIB_HANDLE HMODULE +#define dlopen(name, flags) LoadLibraryA(name) +#define dlsym GetProcAddress +#define dlclose FreeLibrary +EXTERN_C char *dlerror(void); +#else // ! defined _WIN32 +#include +#define LIB_HANDLE void * +#endif // defined _WIN32 + +#endif // ! defined COMPAT_DLFUNC_H_08AEAEE7_485F_4969_8FDF_FBC2E4A78713 + diff --git a/src/config_common.h b/src/config_common.h index daaa8f0cf..1a1dd7f9e 100644 --- a/src/config_common.h +++ b/src/config_common.h @@ -10,4 +10,12 @@ #undef MAX #define MAX(a,b) (((a)>(b))?(a):(b)) +#ifndef EXTERN_C +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C +#endif +#endif // defined EXTERN_C + #endif // defined CONFIG_COMMON_H_A4B25A33_74EC_435F_95DD_9738A7A23EA9 diff --git a/src/host.h b/src/host.h index aecb7d70c..fcc213bef 100644 --- a/src/host.h +++ b/src/host.h @@ -188,12 +188,4 @@ struct NOT_DEFINED_STRUCT_THAT_SWALLOWS_SEMICOLON #define OPTIMIZED_FOR for #endif -#ifndef EXTERN_C -#ifdef __cplusplus -#define EXTERN_C extern "C" -#else -#define EXTERN_C -#endif -#endif // defined EXTERN_C - #endif diff --git a/src/jack_common.h b/src/jack_common.h index 432ee4e34..857a71973 100644 --- a/src/jack_common.h +++ b/src/jack_common.h @@ -39,16 +39,12 @@ #ifndef JACK_COMMON_H #define JACK_COMMON_H -#ifndef _WIN32 -#include -#endif - #include #include #include +#include "compat/dlfunc.h" #include "debug.h" -#include "lib_common.h" #include "types.h" diff --git a/src/lib_common.cpp b/src/lib_common.cpp index b3189f3b2..f9bd06068 100644 --- a/src/lib_common.cpp +++ b/src/lib_common.cpp @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2012-2019 CESNET, z. s. p. o. + * Copyright (c) 2012-2022 CESNET, z. s. p. o. * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/src/lib_common.h b/src/lib_common.h index b3520d536..ed4b12ec3 100644 --- a/src/lib_common.h +++ b/src/lib_common.h @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2011-2021 CESNET, z. s. p. o. + * Copyright (c) 2011-2022 CESNET, z. s. p. o. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,31 +43,6 @@ #include "host.h" // UNIQUE_LABEL -#ifdef _WIN32 -#define LIB_HANDLE HMODULE -#define dlopen(name, flags) LoadLibraryA(name) -#define dlsym GetProcAddress -#define dlclose FreeLibrary -#if !defined __cplusplus && !defined thread_local -#define thread_local _Thread_local -#endif -static char *dlerror(void) ATTRIBUTE(unused); - -static char *dlerror(void) { - thread_local static char buf[1024] = "(unknown)"; - FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags - NULL, // lpsource - GetLastError(), // message id - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid - buf, // output buffer - sizeof buf, // size of msgbuf, bytes - NULL); // va_list of arguments - return buf; -} -#else // ! defined _WIN32 -#define LIB_HANDLE void * -#endif // defined _WIN32 - /** @brief This macro causes that this module will be statically linked with UltraGrid. */ #define MK_STATIC(A) A, NULL #define MK_STATIC_REF(A) &A, NULL diff --git a/src/ndi_common.h b/src/ndi_common.h index 8f58f0b6a..146edf9f3 100644 --- a/src/ndi_common.h +++ b/src/ndi_common.h @@ -43,18 +43,12 @@ #include #include -#ifdef _WIN32 -#include -#else -#include -#endif - #define NDILIB_CPP_DEFAULT_CONSTRUCTORS 0 #include +#include "compat/dlfunc.h" #include "config_common.h" // MAX #include "debug.h" -#include "lib_common.h" // LIB_HANDLE, dlclose/dlerror abstraction #include "utils/color_out.h" // MERGE, TOSTRING #include "utils/misc.h" // MERGE, TOSTRING diff --git a/src/video_capture/ximea.c b/src/video_capture/ximea.c index 308c1fe27..4368a9bdf 100644 --- a/src/video_capture/ximea.c +++ b/src/video_capture/ximea.c @@ -47,11 +47,8 @@ #include #endif -#ifndef WIN32 -#include -#endif - #include "debug.h" +#include "compat/dlfunc.h" #include "lib_common.h" #include "utils/color_out.h" #include "utils/misc.h"