Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.5KB

  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #ifndef __CARLA_LIB_UTILS_HPP__
  18. #define __CARLA_LIB_UTILS_HPP__
  19. #include "CarlaUtils.hpp"
  20. #if defined(CARLA_OS_WIN) && ! defined(__WINE__)
  21. # define CARLA_LIB_WINDOWS
  22. #else
  23. # include <dlfcn.h>
  24. #endif
  25. // -------------------------------------------------
  26. // library related calls
  27. static inline
  28. void* lib_open(const char* const filename)
  29. {
  30. CARLA_ASSERT(filename != nullptr);
  31. #ifdef CARLA_LIB_WINDOWS
  32. return (void*)LoadLibraryA(filename);
  33. #else
  34. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  35. #endif
  36. }
  37. static inline
  38. bool lib_close(void* const lib)
  39. {
  40. CARLA_ASSERT(lib != nullptr);
  41. if (lib == nullptr)
  42. return false;
  43. #ifdef CARLA_LIB_WINDOWS
  44. return FreeLibrary((HMODULE)lib);
  45. #else
  46. return (dlclose(lib) == 0);
  47. #endif
  48. }
  49. static inline
  50. void* lib_symbol(void* const lib, const char* const symbol)
  51. {
  52. CARLA_ASSERT(lib != nullptr);
  53. CARLA_ASSERT(symbol != nullptr);
  54. if (lib == nullptr && symbol == nullptr)
  55. return nullptr;
  56. #ifdef CARLA_LIB_WINDOWS
  57. return (void*)GetProcAddress((HMODULE)lib, symbol);
  58. #else
  59. return dlsym(lib, symbol);
  60. #endif
  61. }
  62. static inline
  63. const char* lib_error(const char* const filename)
  64. {
  65. CARLA_ASSERT(filename != nullptr);
  66. #ifdef CARLA_LIB_WINDOWS
  67. static char libError[2048];
  68. carla_zeroMem(libError, sizeof(char)*2048);
  69. LPVOID winErrorString;
  70. DWORD winErrorCode = GetLastError();
  71. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  72. snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  73. LocalFree(winErrorString);
  74. return libError;
  75. #else
  76. return dlerror();
  77. // unused
  78. (void)filename;
  79. #endif
  80. }
  81. // -------------------------------------------------
  82. #endif // __CARLA_LIB_UTILS_HPP__